Java Utililty Methods Resource Path Get

List of utility methods to do Resource Path Get

Description

The list of methods to do Resource Path Get are organized into topic(s).

Method

URLgetResource(final String path)
get Resource
return Thread.currentThread().getContextClassLoader().getResource(path);
FilegetResource(String path)
get Resource
URL bundleurl = Platform.getBundle("de.schenk.jrtrace.service.test").getEntry(path);
try {
    URL fileURL = FileLocator.toFileURL(bundleurl);
    return new File(fileURL.toURI());
} catch (URISyntaxException e) {
    throw new RuntimeException(e);
} catch (IOException e) {
    throw new RuntimeException(e);
...
URLgetResource(String path)
Loads the resource from the class loader returned by #getClassLoader() .
URL url = getClassLoader().getResource(path);
if (url == null)
    throw new IllegalArgumentException("Unable to find resource at " + path);
return url;
FilegetResource(String relativePath)
get Resource
if (Platform.isRunning()) {
    URL fileURL = Platform.getBundle(PLUGIN_ID).getEntry(relativePath);
    File file = null;
    try {
        if (FileLocator.resolve(fileURL).toString().indexOf(" ") > -1) {
            URL location = FileLocator.resolve(fileURL);
            URI resolvedUri = new URI(location.getProtocol(), location.getPath(), null);
            file = new File(resolvedUri);
...
URLgetResource(String resourcePath)
get Resource
if (resourcePath == null) {
    return null;
resourcePath = formatResource(resourcePath);
URL url = getCurrentLoader().getResource(resourcePath);
return url;
FilegetResourceAsFile(Object relativeTo, String relativePath)
Get a File object from a resource on the classpath using the ClassLoader that loaded the given object.
ClassLoader cl = relativeTo.getClass().getClassLoader();
URL url = cl.getResource(relativePath);
URI uri;
try {
    uri = url.toURI();
    File f = new File(uri);
    return f;
} catch (URISyntaxException e) {
...
FilegetResourceAsFile(String relativePath)
get Resource As File
URL url = Thread.currentThread().getContextClassLoader().getResource(relativePath);
File file = new File(url.toURI());
return file;
InputStreamgetResourceAsStream(final String path)
get Resource As Stream
final URL resource = getResource(path);
if (resource == null) {
    throw new IllegalStateException(path);
return resource.openStream();
InputStreamgetResourceAsStream(String resourcePath, ClassLoader classLoader, Class clazz)
Get the InputStream input stream to the resource given by the supplied path.
if (resourcePath == null)
    throw new IllegalArgumentException("resourcePath may not be null");
InputStream result = null;
if (classLoader != null) {
    result = classLoader.getResourceAsStream(resourcePath);
if (result == null && clazz != null) {
    result = clazz.getResourceAsStream(resourcePath);
...
StringgetResourceAsString(String path)
get Resource As String
try {
    URL resource = Thread.currentThread().getContextClassLoader().getResource(path);
    if (resource != null) {
        return readFromStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));
    } else {
        throw new RuntimeException(new FileNotFoundException(path));
} catch (Exception e) {
...