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.
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 ]
Joined: Jan 29, 2003
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
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?)