wood burning stoves*
The moose likes Java in General and the fly likes How to determine the current path of the classloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to determine the current path of the classloader" Watch "How to determine the current path of the classloader" New topic
Author

How to determine the current path of the classloader

Juri Glass
Greenhorn

Joined: Jul 01, 2009
Posts: 6
Hi

When I do:

resource is null, the Java API says that getResource returns "A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource."

So, how do I determine where the classloader is looking for the resource?

Best
Juri
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

It all depends on the class loader. The default class loader looks at the class path and all JAR / ZIP files in the JAVA_HOME\lib\ext folder. A URLClassLoader looks at the URLs passed to its constructor. Other class loaders may go as far as look in a database.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Juri Glass wrote:Hi

When I do:

resource is null, the Java API says that getResource returns "A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource."

So, how do I determine where the classloader is looking for the resource?

Best
Juri


Try call getResource with a slash - like this

You can also try to call getResource from the class object

or


Regards, Rene Larsen
Dropbox Invite
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

Promoted from Beginner...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Rene Larsen wrote:
Juri Glass wrote:Hi

When I do:

resource is null, the Java API says that getResource returns "A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource."

So, how do I determine where the classloader is looking for the resource?

Best
Juri


Try call getResource with a slash - like this

You can also try to call getResource from the class object

or

That's probably not going to work. ClassLoaders ask their parent to do the work first, then jump in afterwards if necessary. This is not only true for loading classes but also for finding resources.

I first noticed the latter when trying to load a MANIFEST.MF file. Naively I used getResource("/META-INF/MANIFEST.MF"). I surely got a URL - to the manifest file in a JAR file in my JAVA_HOME\lib\ext folder, not the JAR file the class itself was loaded from.

In fact, try the following:
Now go to a different folder, and execute the following command: "java -cp .;<other folder> Test". The results may surprise you.

Of course with this code you may be able to get all paths. I did notice that JAR files are not returned.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to determine the current path of the classloader