Example usage for java.lang Class getResource

List of usage examples for java.lang Class getResource

Introduction

In this page you can find the example usage for java.lang Class getResource.

Prototype

@CallerSensitive
public URL getResource(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:acromusashi.stream.resource.ResourceResolver.java

/**
 * Return target file path from class and resource's path.
 *
 * @param clazz Target class//  ww w.j a  va 2  s.  c  om
 * @param path resource's path
 * @return File object
 */
public static File resolve(Class<?> clazz, String path) {
    URL url = clazz.getResource(path);
    if (url == null) {
        return null;
    }

    File result;
    try {
        result = Paths.get(url.toURI()).toFile();
    } catch (URISyntaxException ex) {
        return null;
    }

    return result;
}

From source file:acromusashi.stream.resource.ResourceResolver.java

/**
 * Return target file path from resource's path.
 *
 * @param path resource's path//from   ww  w  . ja  v a2  s.  c om
 * @return File object
 */
public static File resolve(String path) {
    Class<?> callerClass = resolveCaller();
    URL url = callerClass.getResource(path);
    if (url == null) {
        return null;
    }

    File result;
    try {
        result = Paths.get(url.toURI()).toFile();
    } catch (URISyntaxException ex) {
        return null;
    }

    return result;
}

From source file:com.us.util.FileHelper.java

/**
 * ???<br/>//from  ww w  .  j  a  v  a 2 s . c o  m
 * <ol>
 * <li>~ [?]</li>
 * <li>root: [Web]</li>
 * </ol>
 * 
 * @param path
 * @return
 */
public static File getFile(String path) {
    if (path.startsWith("~")) {
        if (OSHelper.isMac()) {
            return new File(System.getenv().get("HOME") + path.substring(1));
        }
        return new File(System.getenv().get("USERPROFILE") + path.substring(1));
    }
    if (path.startsWith("classpath:")) {
        Class<?> clazz = new FileHelper().getClass();
        String uripath = clazz.getResource(".").getPath();
        String packepath = clazz.getPackage().getName().replace(".", "/").toString();
        return new File(uripath.replace("/" + packepath, "").substring(1) + path.substring(10));
    }
    return new File(path);
}

From source file:com.vmware.qe.framework.datadriven.impl.supplier.XMLDataParser.java

/**
 * Load the data file and convert them in to a list of data's.
 * /* w  w w.  j  a  v a 2  s. co  m*/
 * @throws Exception
 */
public static Map<String, HierarchicalConfiguration> load(String dataFilePath, Class<?> clazz)
        throws Exception {
    HierarchicalConfiguration dataFromFile = null;
    URL dataFileURL = clazz.getResource(dataFilePath);
    log.info("Validating against schema file:");
    validateAgainstSchema(dataFileURL);
    log.info("Reading the data file: " + dataFileURL);
    dataFromFile = new XMLConfiguration(dataFileURL);
    return segregate(dataFromFile, clazz);
}

From source file:de.tudarmstadt.ukp.dkpro.tc.core.util.SaveModelUtils.java

public static void writeFeatureClassFiles(File modelFolder, List<String> featureSet) throws Exception {
    for (String featureString : featureSet) {
        Class<?> feature = Class.forName(featureString);
        InputStream inStream = feature.getResource("/" + featureString.replace(".", "/") + ".class")
                .openStream();//www .ja v a  2s. c  om

        OutputStream outStream = buildOutputStream(modelFolder, featureString);

        IOUtils.copy(inStream, outStream);
        outStream.close();
        inStream.close();

    }

}

From source file:org.wrml.runtime.EngineConfiguration.java

/**
 * Load the EngineConfiguration from a named resource owned by the identified class.
 */// w w  w .  jav a 2 s.c o m
public final static EngineConfiguration load(final Class<?> resourceOwner, final String resourceName)
        throws IOException {

    final URL resource = resourceOwner.getResource(resourceName);
    LOGGER.trace("loading EngineConfiguration from  '{}' [{}]...", resourceName, resource);
    final EngineConfiguration result = EngineConfiguration
            .load(resourceOwner.getResourceAsStream(resourceName));
    LOGGER.debug("loaded EngineConfiguration from  '{}'", resource);
    return result;
}

From source file:com.app.test.BaseTestCase.java

protected static void setUpProperties() throws Exception {
    Class<?> clazz = BaseTestCase.class;

    URL resource = clazz.getResource("/test-config.properties");

    PropertiesUtil.loadConfigurationProperties(resource.getPath());
}

From source file:eu.riscoss.server.DBConnector.java

public static File findLocation(Class<?> cls) {
    String t = cls.getPackage().getName() + ".";
    String clsname = cls.getName().substring(t.length());
    String s = cls.getResource(clsname + ".class").toString();

    s = s.substring(s.indexOf("file:") + 5);
    int p = s.indexOf("!");
    if (p != -1) {
        s = s.substring(0, p);/* w  ww.  j  a  v a  2  s  .c  om*/
    }

    return new File(new File(s).getParent());
}

From source file:Main.java

/**
 * Returns the folder that contains a jar that contains the class
 *
 * @param aclass a class to find a jar// www . ja  v  a2 s . co  m
 * @return
 */
public static String getJarContainingFolderPath(Class aclass) throws Exception {
    CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();

    File jarFile;

    if (codeSource.getLocation() != null) {
        jarFile = new File(codeSource.getLocation().toURI());
    } else {
        String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();

        int startIndex = path.indexOf(":") + 1;
        int endIndex = path.indexOf("!");
        if (startIndex == -1 || endIndex == -1) {
            throw new IllegalStateException(
                    "Class " + aclass.getSimpleName() + " is located not within a jar: " + path);
        }
        String jarFilePath = path.substring(startIndex, endIndex);
        jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
        jarFile = new File(jarFilePath);
    }
    return jarFile.getParentFile().getAbsolutePath();
}

From source file:org.wso2.identity.integration.test.util.Utils.java

public static void setSystemProperties(Class classIn) {
    URL resourceUrl = classIn.getResource(
            File.separator + "keystores" + File.separator + "products" + File.separator + "wso2carbon.jks");
    System.setProperty("javax.net.ssl.trustStore", resourceUrl.getPath());
    System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    System.setProperty("javax.net.ssl.trustStoreType", "JKS");
}