124

I would like to know the best way of loading a resource in Java:

  • this.getClass().getResource() (or getResourceAsStream()),
  • Thread.currentThread().getContextClassLoader().getResource(name),
  • System.class.getResource(name).
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Doc Davluz
  • 4,154
  • 5
  • 30
  • 32

5 Answers5

151

Work out the solution according to what you want...

There are two things that getResource/getResourceAsStream() will get from the class it is called on...

  1. The class loader
  2. The starting location

So if you do

this.getClass().getResource("foo.txt");

it will attempt to load foo.txt from the same package as the "this" class and with the class loader of the "this" class. If you put a "/" in front then you are absolutely referencing the resource.

this.getClass().getResource("/x/y/z/foo.txt")

will load the resource from the class loader of "this" and from the x.y.z package (it will need to be in the same directory as classes in that package).

Thread.currentThread().getContextClassLoader().getResource(name)

will load with the context class loader but will not resolve the name according to any package (it must be absolutely referenced)

System.class.getResource(name)

Will load the resource with the system class loader (it would have to be absolutely referenced as well, as you won't be able to put anything into the java.lang package (the package of System).

Just take a look at the source. Also indicates that getResourceAsStream just calls "openStream" on the URL returned from getResource and returns that.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101
  • AFAIK packages names don't matter, it's the classpath of the class loader that does. – Bart van Heukelom Oct 05 '10 at 09:47
  • @Bart if you look at the source code you'll notice that the class name does matter when you call getResource on a class. The first thing this call does is call "resolveName" which adds the package prefix if appropriate. Javadoc for resolveName is "Add a package name prefix if the name is not absolute Remove leading "/" if name is absolute" – Michael Wiles Oct 05 '10 at 10:22
  • 13
    Ah, I see. Absolute here means relative to the classpath, rather than filesystem absolute. – Bart van Heukelom Oct 05 '10 at 11:55
  • 1
    I just want to add that you should always check that the stream returned from getResourceAsStream() is not null, because it will be if the resource is not within the classpath. – stenix Oct 22 '15 at 11:25
  • Also worth noting is that using the context class loader allows the classloader to be changed at runtime via `Thread#setContextClassLoader`. This is useful if you need to modify the classpath while the program is executing. – Max Jan 26 '16 at 17:06
  • @MichaelWiles What if end user in using jar of the probject? JARs are archives you know then what happens if we specify some resources with an address relative to some class while it is packed in a JAR file? – Alireza Mohamadi Dec 27 '16 at 20:41
  • @AlirezaMohamadi that will be fine. The reason why you use this mechanism to load resources is that it is able to load them even if the specified resource is in a jar. – Michael Wiles Dec 28 '16 at 10:16
  • @MichaelWiles and what if an application needs to create temporal resources? Not in android I mean Java application in general. For example my app wants to generate a certificate as a resource and use it. Does this mechanism work? – Alireza Mohamadi Dec 28 '16 at 11:48
  • @Alireza Mohamadi You shouldn't write anything to Java resources since you never know if it's even possible (it's not if it's a jar you're running). Also, they don't have to be files (it could be some virtual store provided by the container). Instead copy needed resources into tmp folder (there's an util for this starting with Java 7, IIRC) and then modify them to your liking. BTW it's possible to get a resource's file path and it'll even work, e.g. when running unit tests from Eclipse (but don't do it ;)). – szczepanpp May 25 '17 at 13:49
  • For anyone experiencing the same problem. I solved it by using the using the Classloader of the thirdparty class containing the code above and setting it on the current thread. Something like the following: - resource - ---- my.properties ```java Thread.currentThread().setContextClassLoader(org.your.clazz.ProblemCode.class.getClassLoader()); InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties"); ``` – Царь всея мира Jan 04 '23 at 18:47
15

Well, it partly depends what you want to happen if you're actually in a derived class.

For example, suppose SuperClass is in A.jar and SubClass is in B.jar, and you're executing code in an instance method declared in SuperClass but where this refers to an instance of SubClass. If you use this.getClass().getResource() it will look relative to SubClass, in B.jar. I suspect that's usually not what's required.

Personally I'd probably use Foo.class.getResourceAsStream(name) most often - if you already know the name of the resource you're after, and you're sure of where it is relative to Foo, that's the most robust way of doing it IMO.

Of course there are times when that's not what you want, too: judge each case on its merits. It's just the "I know this resource is bundled with this class" is the most common one I've run into.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • skeet: a doubt in the statement "you're executing code in an instance method of SuperClass but where this refers to an instance of SubClass" if we are executing statements inside instance method of super class then "this" will refer to the superclass not the subclass. – Dead Programmer Oct 05 '10 at 08:55
  • 1
    @Suresh: No it won't. Try it! Create two classes, making one derive from the other, and then in the superclass print out `this.getClass()`. Create an instance of the subclass and call the method... it'll print out the name of the subclass, not the superclass. – Jon Skeet Oct 05 '10 at 09:01
  • thanks subclass instance method calls the method of superclass. – Dead Programmer Oct 05 '10 at 09:15
  • 1
    What I'm wondering about is whether using this.getResourceAsStream will only be able to load a resource from the same jar as the this clas is from and not from another jar. By my reckoning, it's the class loader that loads the resource and surely won't be confined from loading from only the one jar? – Michael Wiles Oct 05 '10 at 12:44
  • For anyone experiencing the same problem. I solved it by using the using the Classloader of the thirdparty class containing the code above and setting it on the current thread. Something like the following: - resource - ---- my.properties ```java Thread.currentThread().setContextClassLoader(org.your.clazz.ProblemCode.class.getClassLoader()); InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties"); ``` – Царь всея мира Jan 04 '23 at 18:47
11

I search three places as shown below. Comments welcome.

public URL getResource(String resource){

    URL url ;

    //Try with the Thread Context Loader. 
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if(classLoader != null){
        url = classLoader.getResource(resource);
        if(url != null){
            return url;
        }
    }

    //Let's now try with the classloader that loaded this class.
    classLoader = Loader.class.getClassLoader();
    if(classLoader != null){
        url = classLoader.getResource(resource);
        if(url != null){
            return url;
        }
    }

    //Last ditch attempt. Get the resource from the classpath.
    return ClassLoader.getSystemResource(resource);
}
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 2
    I was looking at the comments in your code and the last one sounds interesting. Aren't all resources loaded from the classpath? And what cases would the ClassLoader.getSystemResource() cover that the above didn't succeeded? – nyxz Feb 15 '15 at 12:25
  • In all honesty, I don't get why you would want to load files from 3 different places. Don't you know where your files are stored ? – bvdb Jun 07 '17 at 11:46
  • Shouldn't 'Loader' be an instance of Classloader? What specific type is that? – Tony May 06 '21 at 06:48
4

I know it really late for another answer but I just wanted to share what helped me at the end. It will also load resources/files from the absolute path of the file system (not only the classpath's).

public class ResourceLoader {

    public static URL getResource(String resource) {
        final List<ClassLoader> classLoaders = new ArrayList<ClassLoader>();
        classLoaders.add(Thread.currentThread().getContextClassLoader());
        classLoaders.add(ResourceLoader.class.getClassLoader());

        for (ClassLoader classLoader : classLoaders) {
            final URL url = getResourceWith(classLoader, resource);
            if (url != null) {
                return url;
            }
        }

        final URL systemResource = ClassLoader.getSystemResource(resource);
        if (systemResource != null) {
            return systemResource;
        } else {
            try {
                return new File(resource).toURI().toURL();
            } catch (MalformedURLException e) {
                return null;
            }
        }
    }

    private static URL getResourceWith(ClassLoader classLoader, String resource) {
        if (classLoader != null) {
            return classLoader.getResource(resource);
        }
        return null;
    }

}
nyxz
  • 6,918
  • 9
  • 54
  • 67
0

I tried a lot of ways and functions that suggested above, but they didn't work in my project. Anyway I have found solution and here it is:

try {
    InputStream path = this.getClass().getClassLoader().getResourceAsStream("img/left-hand.png");
    img = ImageIO.read(path);
} catch (IOException e) {
    e.printStackTrace();
}
Vlad
  • 1,541
  • 1
  • 21
  • 27
  • You should better use `this.getClass().getResourceAsStream()` in this case. If you get a look at the source of the `getResourceAsStream` method, you will notice that it does the same thing than you but in a smarter way (fallback if no `ClassLoader` can be found on the class). It also indicates that you can encounter a potential `null` on `getClassLoader` in your code … – Doc Davluz May 26 '17 at 07:33
  • @PromCompot, as I said `this.getClass().getResourceAsStream()` is not working for me, so I use that works. I think there are some people who can face with problem like mine. – Vlad May 27 '17 at 23:03