• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to determine the current path of the classloader

 
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
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • 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
 
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Promoted from Beginner...
 
Rob Spoor
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
    Bookmark Topic Watch Topic
  • New Topic