• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to determine the current path of the classloader

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Promoted from Beginner...
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic