Class.getResource vs ClassLoader.getResource

The difference?

For this I will create a directory structure like this:

So I will be in c:\foo folder all the time.

The above picture may be misleading because of "tree /f" behaviour.

DocumentTwo.txt is in c:\foo
In c:\foo\bar I have DocumentOne.txt and
In c:\foo\faz I have DocumentThree.txt

In c:\foo\bar I have a which looks like this:

I will compile my code and run from command line:

As seen here, all of our resources are found. 

The first 4 are found by getResource... Notice that when there is no leading "/" in class.getResource, the resource is found relative to where the class itself is.

When Class.getResource is used with a leading "/" , then the path is not relative to class anymore, it is relative to where java is called from.  

However, in ClassLoader.getResource, even tough there is no leading "/" still the root is used. So with ClassLoader.getResource, to be able to reach to DocumentOne.txt, I must use "bar/DocumentOne.txt"

Here is a great explanation by Jon Skeet: