Java Resource Get getResource(final String resourceName, final Class caller)

Here you can find the source of getResource(final String resourceName, final Class caller)

Description

get Resource

License

Open Source License

Declaration

public static URL getResource(final String resourceName, final Class<?> caller) 

Method Source Code

//package com.java2s;

import java.net.URL;

public class Main {
    public static URL getResource(final String resourceName, final Class<?> caller) {
        final String resource;
        if (resourceName.startsWith("/")) {
            resource = resourceName.substring(1);
        } else {//from ww w  .  ja  v  a 2s.c  om
            final Package callerPackage = caller.getPackage();
            if (callerPackage != null) {
                resource = callerPackage.getName().replace('.', '/') + '/' + resourceName;
            } else {
                resource = resourceName;
            }
        }
        final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
        if (threadClassLoader != null) {
            final URL url = threadClassLoader.getResource(resource);
            if (url != null) {
                return url;
            }
        }

        final ClassLoader classLoader = caller.getClassLoader();
        if (classLoader != null) {
            final URL url = classLoader.getResource(resource);
            if (url != null) {
                return url;
            }
        }

        return ClassLoader.getSystemResource(resource);
    }
}

Related

  1. getResource(final Class clazz, final String res)
  2. getResource(final Class aClass, final String aName)
  3. getResource(final String name)
  4. getResource(final String res)
  5. getResource(final String resource)
  6. getResource(String filename)
  7. getResource(String filename)
  8. getResource(String folder, String name)
  9. getResource(String inResource)