List of usage examples for java.lang Class getResource
@CallerSensitive
public URL getResource(String name)
From source file:de.javakaffee.web.msm.integration.TestUtils.java
private static void initLogConfig(final Class<? extends TestUtils> clazz) { final URL loggingProperties = clazz.getResource("/logging.properties"); try {//from w w w.ja va 2 s .c om System.setProperty("java.util.logging.config.file", new File(loggingProperties.toURI()).getAbsolutePath()); } catch (final Exception e) { // we don't have a plain file (e.g. the case for msm-kryo-serializer etc), so we can skip reading the config return; } try { LogManager.getLogManager().readConfiguration(); } catch (final Exception e) { LogFactory.getLog(TestUtils.class).error("Could not init logging configuration.", e); } }
From source file:Main.java
/** * Gets the base location of the given class. * <p>/*from w ww . java 2 s . c o m*/ * If the class is directly on the file system (e.g., * "/path/to/my/package/MyClass.class") then it will return the base directory * (e.g., "file:/path/to"). * </p> * <p> * If the class is within a JAR file (e.g., * "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the * path to the JAR (e.g., "file:/path/to/my-jar.jar"). * </p> * * @param c The class whose location is desired. * @see FileUtils#urlToFile(URL) to convert the result to a {@link File}. */ public static URL getLocation(final Class<?> c) { if (c == null) return null; // could not load the class // try the easy way first try { final URL codeSourceLocation = c.getProtectionDomain().getCodeSource().getLocation(); if (codeSourceLocation != null) return codeSourceLocation; } catch (SecurityException e) { // NB: Cannot access protection domain. } catch (NullPointerException e) { // NB: Protection domain or code source is null. } // NB: The easy way failed, so we try the hard way. We ask for the class // itself as a resource, then strip the class's path from the URL string, // leaving the base path. // get the class's raw resource path final URL classResource = c.getResource(c.getSimpleName() + ".class"); if (classResource == null) return null; // cannot find class resource final String url = classResource.toString(); final String suffix = c.getCanonicalName().replace('.', '/') + ".class"; if (!url.endsWith(suffix)) return null; // weird URL // strip the class's path from the URL string final String base = url.substring(0, url.length() - suffix.length()); String path = base; // remove the "jar:" prefix and "!/" suffix, if present if (path.startsWith("jar:")) path = path.substring(4, path.length() - 2); try { return new URL(path); } catch (MalformedURLException e) { e.printStackTrace(); return null; } }
From source file:com.xhsoft.framework.common.utils.ClassUtil.java
/** * @param clazz/*from w w w .j a v a2 s .c o m*/ * @return String * @author lizj */ public static String getClassPathRoot(Class<?> clazz) { String classPath = StringUtil.replace(clazz.getName(), ".", "/") + ".class"; String classFile = getClassName(clazz) + ".class"; String filePath = clazz.getResource(classFile).toString(); if (filePath.endsWith(classPath)) { filePath = filePath.substring(0, filePath.length() - classPath.length()); } if (filePath.startsWith("jar:")) { filePath = filePath.substring("jar:file:/".length()); if (filePath.endsWith("!/")) { filePath = filePath.substring(0, filePath.lastIndexOf("/", filePath.length() - 2) + 1); } } else if (filePath.startsWith("file:")) { filePath = filePath.substring("file:/".length()); } return filePath; }
From source file:com.gargoylesoftware.htmlunit.WebTestCase.java
private static URL getExpectationsResource(final Class<?> referenceClass, final BrowserVersion browserVersion, final String resourcePrefix, final String resourceSuffix) { final String browserSpecificResource = resourcePrefix + "." + browserVersion.getNickname() + resourceSuffix; URL url = referenceClass.getResource(browserSpecificResource); if (url != null) { return url; }//from w ww. j a v a 2 s .co m final String browserFamily = browserVersion.getNickname().replaceAll("[\\d\\.]", ""); final String browserFamilyResource = resourcePrefix + "." + browserFamily + resourceSuffix; url = referenceClass.getResource(browserFamilyResource); if (url != null) { return url; } // fall back: expectations for all browsers final String resource = resourcePrefix + resourceSuffix; return referenceClass.getResource(resource); }
From source file:com.springsource.tcruntime.embedded.bootexample.ExampleController.java
@RequestMapping("/") public String index() { Class klass = Tomcat.class; String location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class").toString(); Matcher m = pattern.matcher(location); if (m.matches()) { return "You are running tc Server Runtime Embedded Version: " + m.group(2) + System.getProperty("line.separator"); } else {/*from w w w . j a va 2 s . c o m*/ return "You are not running a recognized version of tc Server Embedded" + System.getProperty("line.separator"); } }
From source file:net.sf.jasperreports.engine.util.JRResourcesUtil.java
/** * Attempts to find a resource using a class loader. * <p/>//from w w w . j a v a 2 s. co m * The following sources are tried: * <ul> * <li>the class loader returned by {@link #getClassLoader(ClassLoader) getClassLoader(ClassLoader)}</li> * <li>the context class loader</li> * <li><code>clazz.getClassLoader()</code></li> * <li><code>clazz.getResource()</code></li> * </ul> * * @param location the resource name * @param clsLoader a class loader * @param clazz a class * @return the resource URL if found * @deprecated Replaced by {@link #findClassLoaderResource(String, ClassLoader)}. */ public static URL findClassLoaderResource(String location, ClassLoader clsLoader, Class<?> clazz) { ClassLoader classLoader = getClassLoader(clsLoader); URL url = null; if (classLoader != null) { url = classLoader.getResource(location); } if (url == null) { classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader != null) { url = classLoader.getResource(location); } if (url == null) { classLoader = clazz.getClassLoader(); if (classLoader == null) { url = clazz.getResource("/" + location); } else { url = classLoader.getResource(location); } } } return url; }
From source file:main.java.vasolsim.common.GenericUtils.java
/** * Creates a tree item//from ww w . j av a2s.c o m * * @param resourceLoaderClass * @param title * @param iconLocation * @param imgSize * * @return */ public static TreeItem<String> createTreeItem(Class resourceLoaderClass, String title, String iconLocation, int imgSize) { if (resourceLoaderClass == null || title == null || iconLocation == null || imgSize <= 0) return null; ImageView examsIcon = new ImageView( new Image(resourceLoaderClass.getResource(iconLocation).toExternalForm())); examsIcon.setFitHeight(imgSize); examsIcon.setFitWidth(imgSize); return new TreeItem<String>(title, examsIcon); }
From source file:main.java.vasolsim.common.GenericUtils.java
public static TreeItem<TreeElement> createTreeItem(Class resourceLoaderClass, TreeElement box, String iconLocation, int imgSize) { if (resourceLoaderClass == null || box == null || iconLocation == null || imgSize <= 0) return null; ImageView examsIcon = new ImageView( new Image(resourceLoaderClass.getResource(iconLocation).toExternalForm())); examsIcon.setFitHeight(imgSize);/*from w w w . j ava 2 s . c o m*/ examsIcon.setFitWidth(imgSize); return new TreeItem<TreeElement>(box, examsIcon); }
From source file:info.matsumana.repository.EmployeeRepositoryTest.java
/** * ???????class?????/* www . j a va 2 s. c o m*/ * * @param clazz * @return ? */ String resolvePhysicalDirectory(Class clazz) { return new File(clazz.getResource("").getFile()).getPath() + File.separator; }
From source file:integration.MongoDbSeedRule.java
private URL getClassSpecificResource(Class klazz, String name) { return klazz.getResource(klazz.getSimpleName() + "/" + name); }