Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to determine the current path of the classloader

 
Juri Glass
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64847
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Promoted from Beginner...
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic