tags:

views:

171

answers:

3

I'm building a server that loads modules. Each module is a .jar file. Inside the jar there's all the classes that the module needs. The server needs to read the jar, find the main class (it doesnt have the main method on it is just the class that makes the module work but not as a different program), build an object of that class and store it in a vector of modules so that it can acces to a specific module depending on the job to be done.

How can I do that?

As far as the server, it is listening and receiving request, but there's no modules to delegate the job to, since my vector of modules is empty.

+2  A: 

Here is a network class loader example from Sun. This should cover everything you need.

Try this syntax once you get the URL with you original URLClassLoader

JAR URL

The syntax of a JAR URL is:

jar:<url>!/{entry}

for example:

jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class
jar:file:/export/home/faculty/stansif/public_html/java/applets/image/view.jar!/image.gif

Accessing resources

final java.net.URL url = X.class.getResource ("/dir/image.png");
final ImageIcon icon = new ImageIcon (url);

Also look at this URL http://www.javaworld.com/javaworld/javatips/jw-javatip70.html

Romain Hippeau
thx for this link :Dnow some questions, im trying to load a jar from my local computer, inside the jar theres two packages se.modules, se.protocol i want to load the class Module inside the se.modules package, how should my url be?: file:///home/me/SE.jaror file:///home/me/SE.jar!/se/modules/well ive tried both and it didnt work :/ so i think is in the way i give to the loadClass method the name of the class... the name is Module so the loadClass method should work like this:Class c = loadClass("Module"); but is not working :S
gerardorn
@gerardom try this http://sourceforge.net/projects/jcloader/
Romain Hippeau
uhmm thx ill fight with this some time
gerardorn
@Romain Hippeau i downloaded the jar in there, and its giving me Null pointer Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger¬¬ i guess is not working?
gerardorn
@gerardom I guess it has a dependency on Log4J - Let's see if we can find you something simpler. Hang on
Romain Hippeau
@gerardorn - I updated my post to give you the syntax for a jar url.
Romain Hippeau
@Romain Hippeau i tried with the export/ and still wont work have u tried it with a local jar?
gerardorn
+1  A: 

You should look at existing frameworks, including a lightweight OSGi container. However, the general approach that is taken is to create a classloader for each module, and call into each module using Reflection or a common API -- Runnable and Callable being two good candidates.

Dilum Ranatunga
uhm a classloader for each module? i was hoping that if all modules extended a Module class with a method doJob() that was redefined by every subclass, should be enough to build one loader and make it general. what u think?
gerardorn
Dilum Ranatunga
+1  A: 

You should use Java's ServiceLoader class,

http://java.sun.com/javase/6/docs/api/java/util/ServiceLoader.html

This requires your JAR has a provider configuration file in META-INF/services directory. This is the standard built-in way to load modules in Java. If you need more robust module loading, you might want look at OSGi.

ZZ Coder
this worked like a charm, thx
gerardorn