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 ]
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?)