Java Resource Load getResourceURL(String resourcePath)

Here you can find the source of getResourceURL(String resourcePath)

Description

Get an URL of a classpath-relative resource.

License

Open Source License

Parameter

Parameter Description
resourcePath Path to the resource.

Exception

Parameter Description
IOException when the resource is not found.

Return

URL of resource

Declaration

public static URL getResourceURL(String resourcePath) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.net.URL;

public class Main {
    /**//from  w  ww.  j  av  a 2 s.c o  m
     * Get an URL of a classpath-relative resource. For a resource placed
     * in
     * <pre>src/graphics/xxx/yyy.png</pre>
     * this would be
     * <pre>xxx/yyy.png</pre>
     * 
     * @param resourcePath Path to the resource.
     * @return URL of resource
     * @throws IOException when the resource is not found.
     */
    public static URL getResourceURL(String resourcePath) throws IOException {
        final ClassLoader cl = Thread.currentThread().getContextClassLoader();
        final URL url = cl.getResource(resourcePath);
        if (url == null)
            throw new IOException("Resource not found: " + resourcePath);
        return url;
    }
}

Related

  1. getResourceString(String filename)
  2. getResourceUri(final String packageToScan, final ClassLoader classLoader)
  3. getResourceUrl(final Class aClass, final String aPath)
  4. getResourceUrl(final String resourcePath)
  5. getResourceUrl(String path, String extension, ClassLoader loader)
  6. getResourceUrl(String resourcePath)
  7. getResourceUsingFileStreams(InputStream source)
  8. getResponseCode(URI uri)