Example usage for java.lang ClassLoader getSystemResource

List of usage examples for java.lang ClassLoader getSystemResource

Introduction

In this page you can find the example usage for java.lang ClassLoader getSystemResource.

Prototype

public static URL getSystemResource(String name) 

Source Link

Document

Find a resource of the specified name from the search path used to load classes.

Usage

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

public static ImageIcon createImageIcon(final String icon) {
    if (!cache.containsKey(icon)) {
        final ImageIcon imageIcon = new ImageIcon(ClassLoader.getSystemResource(icon));
        cache.put(icon, imageIcon);//from   w  ww  .  jav  a2 s .  com
        LOGGER.info("cache-miss: put icon for " + icon + " in cache");
        return imageIcon;
    }
    return cache.get(icon);
}

From source file:Main.java

/**
 * Loads an image from within a jar./*from  ww w. j  a v a  2s.c om*/
 * 
 * @param fileName the image path. 
 * @return an initialised image-icon or null, when no image could be found.
 */
public static Image loadIcon(String fileName) {
    URL url = ClassLoader.getSystemResource(fileName);

    if (url == null) {
        System.out.println("unable to locate [" + fileName + "].");
        return null;
    }
    return Toolkit.getDefaultToolkit().createImage(url);
}

From source file:com.siemens.scr.avt.ad.io.BatchLoader.java

protected static void initLogging() {
    try {//from   w ww  . j  a va  2 s  .  c o m
        Properties props = new Properties();
        props.load(ClassLoader.getSystemResource("log4j.properties").openStream());
        PropertyConfigurator.configure(props);
    } catch (Exception e) {
        System.err.println("unable to load log4j.properties");
    }
}

From source file:Textures.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(new Color(212, 212, 212));
    g2d.drawRect(10, 15, 90, 60);/*from   www . j  a v a2 s . c  o  m*/

    BufferedImage bimage1 = null;

    URL url1 = ClassLoader.getSystemResource("a.png");

    try {
        bimage1 = ImageIO.read(url1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    Rectangle rect1 = new Rectangle(0, 0, bimage1.getWidth(), bimage1.getHeight());
    TexturePaint texture1 = new TexturePaint(bimage1, rect1);

    g2d.setPaint(texture1);
    g2d.fillRect(10, 15, 90, 60);
}

From source file:ResourcesUtils.java

/**
 * Returns the URL of the resource on the classpath
 * //from  w  w w .j a va 2 s .c  om
 * @param resource
 *            The resource to find
 * @throws IOException
 *             If the resource cannot be found or read
 * @return The resource
 */
public static URL getResourceURL(String resource) throws IOException {
    URL url = null;
    ClassLoader loader = ResourcesUtils.class.getClassLoader();
    if (loader != null) {
        url = loader.getResource(resource);
    }
    if (url == null) {
        url = ClassLoader.getSystemResource(resource);
    }
    if (url == null) {
        throw new IOException("Could not find resource " + resource);
    }
    return url;
}

From source file:org.apache.hive.common.util.HiveTestUtils.java

public static String getFileFromClasspath(String name) {
    URL url = ClassLoader.getSystemResource(name);
    if (url == null) {
        throw new IllegalArgumentException("Could not find " + name);
    }//  ww  w . ja  v a2 s . co  m
    return url.getPath();
}

From source file:com.zenika.dorm.maven.test.grinder.GrinderGenerateJson.java

public static void generateJsonAndRepository(File repository) {
    try {/*  w  ww  .  j a v a2 s.  co m*/
        URL urlSample = ClassLoader.getSystemResource(SAMPLE_JAR_PATH);
        URL urlJsonFile = ClassLoader.getSystemResource(JSON_PATH);

        LOG.info("Sample URI: " + urlSample.toURI());

        File sample = new File(urlSample.toURI());
        File jsonFile = new File(urlJsonFile.toURI());
        generateJsonAndRepository(repository, sample, jsonFile);
    } catch (URISyntaxException e) {
        throw new RuntimeException("Unable to build Uri with this path: " + repository, e);
    }
}

From source file:com.tunyk.jsonbatchtranslate.api.JsonBatchTranslateTest.java

@BeforeClass
public static void setUp() throws IOException {
    Properties properties = new Properties();
    URL url = ClassLoader.getSystemResource("config.properties");
    properties.load(url.openStream());//w ww .  j  a v  a2 s . c  o  m
    googleTranslateApiKey = properties.getProperty("google.translate.api.key");
    microsoftTranslatorApiKey = properties.getProperty("microsoft.translator.api.key");
    yandexApiKey = null;
    in = FileUtils.readFileToString(new File(ClassLoader.getSystemResource("input.json").getPath()));
}

From source file:com.cloud.utils.PropertiesUtil.java

/**
 * Searches the class path and local paths to find the config file.
 * @param path path to find.  if it starts with / then it's absolute path.
 * @return File or null if not found at all.
 */// w  w w.  j a v  a  2 s.  co  m

public static File findConfigFile(String path) {
    ClassLoader cl = PropertiesUtil.class.getClassLoader();
    URL url = cl.getResource(path);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    url = ClassLoader.getSystemResource(path);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    File file = new File(path);
    if (file.exists()) {
        return file;
    }

    String newPath = "conf" + (path.startsWith(File.separator) ? "" : "/") + path;
    url = ClassLoader.getSystemResource(newPath);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    url = cl.getResource(newPath);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    newPath = "conf" + (path.startsWith(File.separator) ? "" : File.separator) + path;
    file = new File(newPath);
    if (file.exists()) {
        return file;
    }

    newPath = System.getProperty("catalina.home");
    if (newPath == null) {
        newPath = System.getenv("CATALINA_HOME");
    }

    if (newPath == null) {
        newPath = System.getenv("CATALINA_BASE");
    }

    if (newPath == null) {
        return null;
    }

    file = new File(newPath + File.separator + "conf" + File.separator + path);
    if (file.exists()) {
        return file;
    }

    return null;
}

From source file:Main.java

/**
 * This method will search for <code>resource</code> in different
 * places. The search order is as follows:
 * <ol>//w  w w  . jav  a2 s.co m
 * <p><li>Search for <code>resource</code> using the thread context
 * class loader under Java2.
 * <p><li>Try one last time with
 * <code>ClassLoader.getSystemResource(resource)</code>, that is is
 * using the system class loader in JDK 1.2 and virtual machine's
 * built-in class loader in JDK 1.1.
 * </ol>
 * <p/>
 *
 * @param resource
 * @return TODO
 */
public static URL getResource(String resource) {
    ClassLoader classLoader = null;
    URL url = null;
    try {
        classLoader = getTCL();
        if (classLoader != null) {
            url = classLoader.getResource(resource);
            if (url != null) {
                return url;
            }
        }
    } catch (Throwable t) {

    }

    // Last ditch attempt: get the resource from the class path. It
    // may be the case that clazz was loaded by the Extension class
    // loader which the parent of the system class loader. Hence the
    // code below.

    return ClassLoader.getSystemResource(resource);
}