remote jars in the classpath

情到浓时终转凉″ 提交于 2019-12-05 15:24:23

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 :)

You could use an URLClassLoader, but it would download the file every time, and would make the code more complex.

If you're using a shell script already, why don't you simply use curl to download the jar and place it in the classpath?

Class loading is a complex process. It's possible that the regular classpath ClassLoader is a URLClassLoader in all runtime environments on all platforms, but I assume it wouldn't necessarily have to be.

One method for adding classpath entries is to add a Class-Path: property to a jarfile's META-INF/MANIFEST.MF file, and the space-separated values of that property are resolved with a URLClassLoader. (Maven adds some of its classpath entries to jarfile manifests as file:// URIs, implying that http:// or https:// would work too.) So even if you can't get URL-based classpath entries working in the normal Java classpath in some runtime environment, you should be able to get them working by specifying the URL in a manifest file.

(I'm not familiar with how Java WebStart works, but maybe that also makes use of URL-based classpath entries?)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!