简体   繁体   English

类路径中的远程jar

[英]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, ie, entries like CLASSPATH="http://somewhere.net/library.jar:...". 我想知道是否有一些已知的Java类加载器能够接受类路径中的远程文件,即CLASSPATH =“http://somewhere.net/library.jar:...”等条目。

Note that I am not talking about applets or Java Web Start. 请注意,我不是在谈论applet或Java Web Start。 Think of an application that can use different back-ends (eg, 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. 想想可以使用不同后端的应用程序(例如,MySQL,Oracle),我想根据用户的后端首选项在shell脚本中准备类路径,并让类加载器下载所需的来自分发服务器的jar(本例中为jdbc驱动程序)。 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). 我也不是在谈论Maven(用户只是得到二进制分发,我不想强​​迫他们从源代码构建他们需要的东西)。

The SystemClassLoader is a URLClassLoader . SystemClassLoaderURLClassLoader 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. 您可以使用URLClassLoader ,但它每次都会下载文件,并使代码更复杂。

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? 如果您已经使用了shell脚本,为什么不使用curl下载jar并将其放在类路径中?

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. 常规类路径ClassLoader可能是所有平台上所有运行时环境中的URLClassLoader,但我认为它不一定非必要。

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. 添加类路径条目的一种方法是将一个Class-Path:属性添加到jarfile的META-INF/MANIFEST.MF文件中,并使用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. (Maven将一些类路径条目添加到jar file://清单中作为file:// URIs,暗示http://https://也可以工作。)因此,即使您无法获取基于URL的类路径条目在某些运行时环境中,您应该能够通过在清单文件中指定URL来使它们正常工作。

(I'm not familiar with how Java WebStart works, but maybe that also makes use of URL-based classpath entries?) (我不熟悉Java WebStart的工作原理,但也许这也使用了基于URL的类路径条目?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM