views:

308

answers:

3

I want a mechanism that will allow to dynamically load and unload jars as well as calling an activator class in the jar. I don't want to use OSGi because of the cumbersome import/export mechanism.

A: 

JavaRebel, but it is mostly appropriate for development, not a production deployment. If you are looking for an open source alternative, you can follow this question.

Yishai
+1  A: 

You can use the ClassLoader to dynamically load classes from JAR files. When you have dynamically loaded the class, you can explicitly invoke an initialization routine if necessary. Like the rest of Java, this mechanism is garbage-collected, so there is no way to explicitly unload the JAR; it is up to the JVM to release the JAR when the dynamically loaded classes are no longer referenced and garbage collection has run.

Michael Aaron Safyan
Unloading is not quite that simple. All objects contain an implicit reference to their Class object, which contains a reference to the ClassLoader, which contains references to all the classes it loaded. You have to make sure all of that can be garbage collected before any of it can.
Adam Crume
@Adam, reread my answer. I stated that there is no way to explicitly unload the JAR and that it cannot be released until "the dynamically loaded classes are no longer referenced"; I never suggested that the classes will be no longer referenced while instances of the classes still exist. Obviously if there are instances of the class still in existence, then the class will still be referenced.
Michael Aaron Safyan
+1  A: 

There's a project called the Java Plugin Framework that might be what you're looking for. On their web page it states the project is an attempt to match and extend Eclipse's pre-OSGI plugin architecture. It's a generic framework and isn't tied to SWT/desktop apps.

Nate