| Author |
Designing a pluggable application.
|
Landon Blake
Ranch Hand
Joined: Oct 15, 2004
Posts: 43
|
|
I am in the midst of designing a Java application that will use plug-ins to extend its functionality. I have figured out that I can use the Class object and its forName() and newInstance() methods to create objects from classes stored in a plug-in JAR. Here is my question: All of my plug-ins will store there JAR files in a single directory of my application directory on the file system. How do I get the JVM to search in this plug-in directory when I use the forName() method of the Class object? Do I have to get the user to add the directory to there classpath, or is there another way around this? I didn't see a way to pass the location of the plug-in directory of my application when using the forName() method. Thanks, Landon P.S. - When I load a class in this way, are all classes in the JAR loaded, or just the one I specify? If it is just the one I specify, how does the object created call other objects in its JAR? If all classes are loaded, how do I unload them when a plugin is no longer needed in my application? [ March 08, 2006: Message edited by: Landon Blake ]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
You can cause each jar file to contain a configuration file that tells the plugins it contains for whatever interfaces you have. Or the file could be outside the jar. Take a look at Eclipse plugins. There is some kind of configuration file in each directory in the plugins area. Once you have the configuration (the jar, the interface and the implementing class) you can use a ClassLoader with a modified classpath to create objects with Class.forName().newInstance(). Any of those sound useful? [ March 08, 2006: Message edited by: Stan James ]
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Stefan Wagner
Ranch Hand
Joined: Jun 02, 2003
Posts: 1923
|
|
Do I have to get the user to add the directory to their classpath?
Yes and no. The user has to add the jar-file to the classpath, or a directory containing extracted classes (with their subdirectories). The classpath is a list of things, which contain classes. That might be jars or directories, but not directories containing jars. (It took me 15 years to understand that simple thing ) I don't have an idea of how to restrict the searching to a part of the classpath. You could create a script to extract all classnames, and tell the user to run it after putting new plugins there: and search that list. Perhaps the easiest solution.
If it is just the one I specify, how does the object created call other objects in its JAR?
That's the job of the classloader. He will do it.
If all classes are loaded, how do I unload them when a plugin is no longer needed in my application?
Job of the GarbageCollection. Let it do it.
|
http://home.arcor.de/hirnstrom/bewerbung
|
 |
Landon Blake
Ranch Hand
Joined: Oct 15, 2004
Posts: 43
|
|
Thanks Stefan and Stan for your quick responses. I was able to answer some of my questions while reading online. It turns out that you can specify an application specific class path when you launch a Java application. I can build this into the script file that launches my application. (A Java application launched this way still has access to the system classes.) If what Stefan said was correct, I'll have to figure out how to set the classpath to each JAR in the directory, and not the directory itself. This might be possible, as I will know ahead of time what JARS I will need to add to the classpath. I have to find out how to modify the classpath during runtime. (Or maybe use a custom class loader?) Thanks for the help. Landon [ March 08, 2006: Message edited by: Landon Blake ]
|
 |
 |
|
|
subject: Designing a pluggable application.
|
|
|