• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Identify jar that a class is embedded into?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might be a weird question..

Whilst executing a class, is it possible to identify its jar or its location (file system or URI)

I have an issue where I have a class in 2 different jar's and not sure which one is getting picked and executed by the classloaders.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Convert the fully qualified Class name into a file name (ending with .class) then use the ClassLoader to find the URL to that file. The URL is able to provide a reference to a location within the JAR, so you will get an answer in the form of <path-to-jar>:<path to file> or something like that.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java classes are generally associated with a ProtectionDomain, which has a CodeSource, which has a location as a URL.

Try:

 
Sheriff
Posts: 22682
128
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

Originally posted by David O'Meara:
Convert the fully qualified Class name into a file name (ending with .class) then use the ClassLoader to find the URL to that file. The URL is able to provide a reference to a location within the JAR, so you will get an answer in the form of <path-to-jar>:<path to file> or something like that.


Correct. Consider the following:

Output:
jar:file:/C:/Program%20Files/Java/jre1.6.0_05/lib/rt.jar!/java/lang/String.class

That is built as follows:
jar: - to indicate it is in a JAR file
file:/C:/Program%20Files/Java/jre1.6.0_05/lib/rt.jar - a URL to the JAR file itself
! - to indicate it is inside a JAR file
/java/lang/String.class - the file inside the JAR file

So if you cut off the jar: and everything after !, then you get a URL to the file. Convert it to a File using new File(url.toURI()).
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic