Search code examples
javaclasspathclassloaderremoteobject

remote jars in the classpath


Sorry, maybe this question is too silly or already answered, but I couldn't find it out.

I'm wondering if there is some known Java class-loader that is able to accept remote files in the classpath, i.e., entries like CLASSPATH="http://somewhere.net/library.jar:...".

Note that I am not talking about applets or Java Web Start. Think of an application that can use different back-ends (e.g., MySQL, Oracle), I'd like to prepare the classpath in a shell script, based on the user's back-end preference and have the class-loader to download the needed jar (the jdbc driver in this example) from a distribution server. I'm not talking about Maven either (the user just gets the binary distribution, I don't want to force them to build what they need from the sources).


Solution

  • The SystemClassLoader is a URLClassLoader. You could try, I leave it to you:

    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
    method.setAccessible(true);
    method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{new URL("http://somewhere.net/library.jar")});  
    Class.forName("your.remote.ClassName");
    

    Let me know :)