• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Detect presence of class file in JAR...

 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic