Dynamic Classloading Challenge

Let me start by briefly stating what we are trying to achieve: we have developed a simulator for a particular system which includes controller class that provides the logic for behavior of one of the system's component. We have several individuals developing different versions of this class which need to be centrally tested in the simulator in order to compare their performance.

Ideally, several versions of the Controller class could be loaded so that different versions could be executed simulataneously and compared. The simulator user would ideally simply navigate to one of the Controller classes, select it in a file chooser dialogue, load it and initiate a simulation run, then continue to the next, load it, etc...

I have defined an Interface for that Controller class but am quite stumped at how to implement a dynamic class loader for these classes. I've tried using the system class loader, defining a custom class loader, using URLClassLoader, etc... To no avail...

Perhaps someone could help me with the general strategy to use here including the programming approach to load several classes all of which conform to a defined Interface (say "IController").

Thanks in advance.

[1224 byte] By [chris321a] at [2007-11-27 2:36:49]
# 1

Maybe it is overkill for your situation, but you may want to look at [url=http://www.osgi.net/]OSGi[/url], as implemented by [url=http://incubator.apache.org/felix/]Felix[/url] or [url=http://www.eclipse.org/equinox/]Equinox[/url] (among others).

OSGi allows bundles (basically jar files with some specifc metadata) to be dynamically added, removed and updated without restarting the application.

Herko_ter_Horsta at 2007-7-12 2:56:27 > top of Java-index,Java Essentials,Java Programming...
# 2
what didn't work about URLClassLoader?
georgemca at 2007-7-12 2:56:27 > top of Java-index,Java Essentials,Java Programming...
# 3

Here is the code I tried for URL Class loader. Couple of points:

- IController is the Interface for the Controller class

- I still haven't figured out how to load the file with a class name taken from the actual .class file (versus what I did below which is explicitly state the name "Controller1".

I've also attached the error - it barfs when it hits the last call which is one of the interface methods that simply returns a name from one of the methods in the class as defined in the interface.

Thanks again for any assistance.

File file=null;

JFileChooser fc = new JFileChooser(file);

JavaFileFilter fileFilter = new JavaFileFilter();

Boolean bail = false;

IController controlObject = null;

fc.addChoosableFileFilter(fileFilter);

fc.setFileFilter(fileFilter);

fc.setDialogTitle("Open the Scenario File");

fc.setMultiSelectionEnabled(false);

fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

fc.setAcceptAllFileFilterUsed(false);

if (fc.showOpenDialog(this.desktop) == JFileChooser.APPROVE_OPTION) file = fc.getSelectedFile();

else bail = true;

if (!bail) {

System.out.println(file.getPath());

try {

// Convert File to a URL

URL url = (file.toURI()).toURL();

URL[] urls = new URL[]{url};

// Create a new class loader with the directory

ClassLoader cl = new URLClassLoader(urls);

try {

controlObject = (IController)(cl.loadClass("Controller1").newInstance());

} catch (InstantiationException e1) {

e1.printStackTrace();

} catch (IllegalAccessException e1) {

e1.printStackTrace();

}

} catch (MalformedURLException e1) {

} catch (ClassNotFoundException e1) {

}

System.out.println(controlObject.getName());

}

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at elevator.Console.actionPerformed(Console.java:319)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

chris321a at 2007-7-12 2:56:27 > top of Java-index,Java Essentials,Java Programming...