aspose file tools*
The moose likes Java in General and the fly likes Detect presence of class file in JAR... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Detect presence of class file in JAR..." Watch "Detect presence of class file in JAR..." New topic
Author

Detect presence of class file in JAR...

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
I'm working on a simple plug-in system for a Java program. I can't seem to find a way to determine if a JAR file contains a class that implements a particular interface. I need to be able to do discover this programmatically. If the JAR file does contain an implementation of the interface, I need to be able to obtain the Class object for that implementation. I've been reading online, but haven't quite found what I need.

I'm guessing it involves using the getEntry() method of the java.util.jar.JarFile class. Is this correct?

After the Entry object is returned by getEntry(), how do I convert it to a Class file?

Thanks,

Landon
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

I would skip the part that required detailed knowledge of how to use a jar file in favour of just pointing a URLClassLoader at said jar file and using it to load classes.

Then once you have loaded the class in question, you can use the interface's isAssignableFrom method to see if an instance of the class can be assigned to a variable of the interface's type.
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
Paul,

Thanks for your quick response.

Here is the problem with what you suggested. I won't know ahead of time which class in the JAR file implements the interface. I'm looking for a way to programmatically discover if there is a class in the JAR that implements the interface. If there is, I want to load that class and create an instance of it.

Any suggestions on that?

Landon
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42624
    
  65
After the Entry object is returned by getEntry(), how do I convert it to a Class file?


You'd use the "getInputStream(ZipEntry)" method, through which you can obtain the bytes that make up the class file. Then you need to have your own ClassLoader (not a big deal to implement), which calls the defineClass method that can create a Class object from those bytes.


Ping & DNS - my free Android networking tools app
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The above does sound viable, but it's a bit of a hassle to implement and probably rather slow to run (got to load all the classes to find the right one).

What about saying that one of the requirements of being a plugin for your system is that a Jar must contain a properties file with a well-known name. That properties file could then contain the name of the class that implements the interface. You could invent some other config that's stored in there, too, should your application require it, now or in the future.

With this proposal, you only have to write code to find one fixed-name resource in a Jar, read it as Properties and load a named class. It will be easier to write and much faster to execute.
[ April 05, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

You are describing standards that already exist in Java, although they are not well publicized, so people keep having to reinvent this wheel. Here is an old version of the JAR specification; in particular read the section headed "Service Provider".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detect presence of class file in JAR...