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???
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.
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.
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.
If you're gonna buy things, buy this thing and I get a fat kickback: