I tried implementing my own class loader based on some examples. However, I think it is not working as expected ( unable to reload Jar file etc. I see few references of recommending using OSGI or Apache Felix for handling Jar file loading. Is there any examples of loading Jar, instancing a class from the Jar?
More details on what I am trying to accomplish..I have a Java command line application that essentially continuously runs. I want it to be able to reference JAR files dynamically at runtime and at run-time instance a class in these jars. These jars may contain 1 or more supporting classes. These Jars are essentially customized work units that get executed by certain event conditions from the main application that is continuously running. .. Since this is a multi-client, I wanted to have the jars be a pluginable type work units.
My current direction has been providing the 'client' an interface to code to and then having them package their classes in a jar file. The application will then execute the configured ( database ) jar path and run class from the Jar. This works with loading of a jar and class however, I want to be able to essentially hot deploy these jars.
In summary I would like to have a JAR file that contains classes to support a function. A defined class that will be referenced at run-time from the Jar ( from the main looping application ). The ability to change out JAR files while the main application is running.
If I am to use a third party library, it is preferred that I use Apache Felix.
thanks
I think I figured it out using Apache Felix 4. Is this the best way to load jars/classes ? Or is there a better more efficient way. My research so far did not point to one solution. thanks.
FrameworkFactory ff = new FrameworkFactory ();
Map<String,Object> config = new HashMap<String,Object>();
config.put("org.osgi.framework.storage", "c:\\temp\\myclientBundles");
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"packages needed,more packages needed");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
Framework fwk = ff.newFramework(config);
fwk.start();
BundleContext bc = fwk.getBundleContext();
Bundle bundle = bc.installBundle("file:C:\\my_client.jar");
bundle.start();
bundle.update();
Class clazz = bundle.loadClass("client.work.process");
Job pc = (Job) clazz.newInstance();
pc.startWork(someConfigObject);
bundle.stop();
fwk.stop();