What is the difference between Class.getResource() and ClassLoader.getResource()?

The first call searches relative to the class file while the latter searches relative to the classpath root.

The first call searches relative to the . Class file while the latter searches relative to the classpath root. To debug issues like that, I print the URL: System.out.

Println( getClass. GetResource(getClass().getSimpleName() + '. Class') ).

I think "classloader root" would be more accurate than "classpath root" - just to be picky. – Jon Skeet Jul 7 at 10:14 1 Both can search "absolute paths" if the file name is prepended by "/" – oligofren Jul 7 at 15:59.

Had to look it up in the specs: download.oracle.com/javase/1.4.2/docs/ap... download.oracle.com/javase/1.4.2/docs/ap... Class's getRessource() - documentation states the difference: This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting ". " to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.

GetSystemResource.

This was the main difference between the two methods when when first looking up an input file, and then creating a file using that in the same directory. The Class version did not find it, the ClassLoader version did (both using "/file. Txt").

– oligofren Jul 7 at 16:22.

Class. GetResource can take a "relative" resource name, which is treated relative to the class's package. Alternatively you can specify an "absolute" resource name by using a leading slash.

Classloader resource paths are always deemed to be absolute. So there are basically equivalent: foo.bar.Baz.class. GetResource("xyz.

Txt"); foo.bar.Baz.class.getClassLoader(). GetResource("foo/bar/xyz. Txt"); And so are these (but they're different to the above :) foo.bar.Baz.class.

GetResource("/data/xyz. Txt"); foo.bar.Baz.class.getClassLoader(). GetResource("data/xyz.

Txt").

I felt free to add the brackets (). However: +1 – Martijn Courteaux Jul 7 at 10:10 @Martijn: Whoops, thanks :) – Jon Skeet Jul 7 at 10:13 Nice answer with clear examples. Although the post actually was meant to get answers to two questions, I see now that the second questions is sort of hidden.

Quite unsure of how/whether I should update the post to reflect this, but what I would like to know second is this (next comment): – oligofren Jul 7 at 16:04 Is there some kind of caching going on in the Class.getResource() version? What caused me to believe this is the generation of some jasper reports: We use getClass(). GetResource("/aDocument.

Jrxml") to fetch the jasper xml file. A binary jasper file is then produced in the same directory. GetClass().

GetResource("/aDocument. Jasper") is not able to find it, although it can clearly find documents at the same level (the input file). This is where ClassLoader.getResource() proved helpful, as it seems it does not use caching of the directory listing.

But I cannot find documentation on this. – oligofren Jul 7 at 16:08 @oligofren: Hmm... I wouldn't expect Class.getResource() to do any caching there... – Jon Skeet Jul 7 at 16:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions