Remote Banks on the Way to Classes

Sorry, maybe this question is too stupid or already answered, but I could not find it.

I am wondering if there is some well-known Java classloader capable of accepting remote files in the classpath, i.e. entries such as CLASSPATH = "http: //somewhere.net/library.jar: ...".

Note that I'm not talking about applets or Java Web Start. Think of an application that can use different back-end (for example, MySQL, Oracle), I would like to prepare the path to the classes in the shell script based on user preferences and load the class loader necessary jar (jdbc driver in this example) from the server distribution. I'm not talking about Maven either (the user just gets the binary distribution, I don't want to force them to create what they need from the sources).

+4
source share
3 answers

SystemClassLoader - URLClassLoader . You can try, I will 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:)

+1
source

You can use URLClassLoader , but it will load the file every time and make the code more complicated.

If you're already using a shell script, why don't you just use curl to load the jar and put it in the classpath?

+1
source

Class loading is a complex process . It is possible that the regular classpath ClassLoader is a URLClassLoader in all runtimes on all platforms, but I assume that this does not have to be.

One way to add entries to the classpath is to add the Class-Path property : to the jarfile META-INF/MANIFEST.MF file and space-separated values โ€‹โ€‹of this property are resolved using the URLClassLoader. (Maven adds some of its classpath entries to jarfile manifests as file:// URI, implying that the http:// or https:// tags will work as well.) That way, even if you cannot get class class entries on Based on URLs that work in the normal Java classpath in some runtime environment, you should be able to get them to work by specifying the URL in the manifest file.

(I'm not familiar with how Java WebStart works, but maybe it also uses URL-based entries?)

+1
source

All Articles