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:strat.mining.stratum.proxy.configuration.ConfigurationManager.java

/**
 * Return the version of the program//from   ww w.j  a  v  a2 s .c  o  m
 * 
 * @return
 */
public static String getVersion() {
    if (version == null) {
        version = "Dev";

        Class<Launcher> clazz = Launcher.class;
        String className = clazz.getSimpleName() + ".class";
        String classPath = clazz.getResource(className).toString();
        if (classPath.startsWith("jar")) {
            // Class not from JAR
            String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1)
                    + "/META-INF/MANIFEST.MF";

            try {
                Manifest manifest = new Manifest(new URL(manifestPath).openStream());
                Attributes attr = manifest.getMainAttributes();
                version = attr.getValue("Implementation-Version");
            } catch (IOException e) {
                // Do nothing, just return Unknown as version
                version = "Unknown";
            }
        }
    }

    return version;
}

From source file:org.xmlactions.common.io.ResourceUtils.java

/**
 * Get a URL for a given resource. The resource must exist or an exception
 * is caused./*  w w  w.j av a 2s  .c om*/
 * 
 * @param resourceName
 * @return the URL of the resource.
 */
public static URL getResourceURL(ApplicationContext appContext, String resourceName, Class<?> clas) {
    URL resource = null;
    try {
        if (appContext != null) {
            resource = appContext.getResource(resourceName).getURL();
        } else {
            ClassPathResource cpr = new ClassPathResource(resourceName);
            resource = cpr.getURL();
        }

        if (resource == null) {
            resource = ResourceUtils.class.getResource(resourceName);
            if (resource == null) {
                resource = clas.getResource(resourceName);
            }
        }
    } catch (IOException ex) {
        // TODO Auto-generated catch block
        throw new IllegalArgumentException("Unable to load resource [" + resourceName + "]", ex);
    }
    Validate.notNull(resource, "Unable to load resource [" + resourceName + "]");

    return resource;
}

From source file:cc.creativecomputing.io.CCIOUtil.java

/**
 * Returns the path to a resource based on the given class, this looks for files in the package
 * of the given class and allows to place files that are needed to work with the code for example
 * shaders to placed with the source code. This is still experimental and need to be checked if things
 * are put together in jar for example.//from  w w  w.ja va2s .co  m
 * @param theClass class to look for a resource
 * @param thePath path inside the class folder
 * @return path based on the given class
 */
static public String classPath(Class<?> theClass, String thePath) {
    URL myResult = theClass.getResource(thePath);
    if (myResult == null) {
        throw new CCIOException("The given Resource is not available:" + theClass.getResource("") + thePath);
    }
    //      CCLog.info("CLASSPATH:" + myResult.getPath().replaceAll("%20", " "));
    return myResult.getPath().replaceAll("%20", " ");
}

From source file:org.callimachusproject.fluid.consumers.HttpJavaScriptResponseWriter.java

private String getSystemId(String frag) {
    Class<?> dclass = this.getClass();
    String name = dclass.getSimpleName() + ".class";
    URL url = dclass.getResource(name);
    if (url != null)
        return url.toExternalForm() + "#" + frag;
    return "java:" + dclass.getName() + "#" + frag;
}

From source file:Main.java

/**
 * Load a given resource. <p/> This method will try to load the resource
 * using the following methods (in order):
 * <ul>// w w  w .  ja v  a2  s .  co m
 * <li>From Thread.currentThread().getContextClassLoader()
 * <li>From ClassLoaderUtil.class.getClassLoader()
 * <li>callingClass.getClassLoader()
 * </ul>
 * 
 * @param resourceName The name of the resource to load
 * @param callingClass The Class object of the calling object
 */
public static URL getResource(String resourceName, Class callingClass) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = Thread.currentThread().getContextClassLoader().getResource(resourceName.substring(1));
    }

    ClassLoader cluClassloader = Main.class.getClassLoader();
    if (cluClassloader == null) {
        cluClassloader = ClassLoader.getSystemClassLoader();
    }
    if (url == null) {
        url = cluClassloader.getResource(resourceName);
    }
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = cluClassloader.getResource(resourceName.substring(1));
    }

    if (url == null) {
        ClassLoader cl = callingClass.getClassLoader();

        if (cl != null) {
            url = cl.getResource(resourceName);
        }
    }

    if (url == null) {
        url = callingClass.getResource(resourceName);
    }

    if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
        return getResource('/' + resourceName, callingClass);
    }

    return url;
}

From source file:com.taobao.adfs.util.Utilities.java

public static String getClassPath(Class<?> clazz) {
    return clazz.getResource("/" + clazz.getName().replaceAll("\\.", "/") + ".class").getPath();
}

From source file:junit.org.rapidpm.microdao.HsqlDBBaseTestUtils.java

public void initSchemaBaseSchema(Class clazz) {
    System.out.println("initSchemaBaseSchema - clazz = " + clazz);
    executeSqlScript(clazz.getResource("CLEAR_SCHEMA.sql").getPath());
    executeSqlScript(clazz.getResource("CREATE_TARGET_DB.sql").getPath());
    executeSqlScript(clazz.getResource("INSERT_BASIC_DATA.sql").getPath());
}

From source file:ijfx.service.ui.DefaultHintService.java

@Override
public void displayHints(Class clazz, boolean force) {
    logger.info("Trying to hint widget " + clazz.getSimpleName());
    try {/*from ww w . j  a va  2  s.c om*/
        String url = clazz.getSimpleName() + "Hints.json";

        if (clazz.getResource(url) == null)
            return;

        List<DefaultHint> hintList = jsonToHintList(TextFileUtils.readFileFromJar(url, clazz));
        hintList.forEach(hint -> hint.setId(clazz.getSimpleName() + hint.getTarget()));
        logger.info(String.format("%s had %d hints loaded", clazz.getSimpleName(), hintList.size()));
        displayHints(hintList, force);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

}

From source file:junit.org.rapidpm.microdao.HsqlDBBaseTestUtils.java

public void initSchemaTestSchema(Class clazz) {
    System.out.println("initSchemaTestSchema - clazz = " + clazz);
    final URL testSqlResource = clazz.getResource(clazz.getSimpleName() + ".sql");
    if (testSqlResource != null) {
        String testSqlPath = testSqlResource.getPath();
        executeSqlScript(testSqlPath);/*from w  w  w  . j  a  v  a 2  s . c om*/
    } else {
        System.out.println("No SQL for " + clazz.getSimpleName());
    }
}