• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applying reflection on jar files

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My question is quit simple, I want to use reflection on jar files without extracting them. Actually i am developing a kind of Class browser.
I want to get complete info about classes packaged in jar file without extracting them and with maximum performance.
Any oe with really nice ideas???
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use reflection on anything on your classpath, it doesn't matter if it's packaged in a classfile.

I do not think there's a way to influence the classpath of an application once it's loaded, so you may be unable to load a jar into your application and analyse it.
Maybe System.setProperty() can be used to append it to the classpath at runtime, I've not tried that.
 
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
You could create your own URLClassLoader that includes only the JAR you're interested in, but this isn't really necessary if the JAR is already on the CLASSPATH.

Trying to modify the classpath at runtime using setProperty won't work, the property appears to be read-only and the change is ignored.

The problem you'll encounter is that you can only use reflection on classes loaded by a ClassLoader, and you can only load classes by their fully qualified names. If your aim is to find all the classes in a JAR file, load them and output information using reflection, first you need to find the fully qualified names of all classes in the JAR. There isn't a direct way to do this.

You'll probably have to use the java.util.zip functionality to find all files in the JAR, filter the ZipEntries for .class files, convert the path and name into a fully qualified class name, then get a reference to the Class through a ClassLoader. Sounds like a lot of work but it shouldn't be too hard.

Tell us how you go?

Dave
 
Umair Nasir
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI!
thanx for replying.
I solved the problem of loading classes from jar files by
first getting jar entries and then making out of them
jarURLs
then use jarURLClass loader

Now there is another task relating to it
How can i read directory structure from a jar file efficiently
(meaning not by tockenizing the jar entries names)

As all zip utilities and jar tool also reads out the directory structure
not actually going through all the entries(a lot of overhead if we are reading rt.jar)

what i want is first read directories at level 1 and making tree nodes at level 1
then i ll go on subsequent directory levels and make new tree nodes ,all at runtime and on demand.
Plz do reply as soon as possible.
I have to submit it after 2 days.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic