类路径中的远程jar [英] remote jars in the classpath

查看:137
本文介绍了类路径中的远程jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,也许这个问题太傻或者已经回答了,但是我找不到它。



我想知道是否有一些已知的Java类-loader,它能够接受类路径中的远程文件,即CLASSPATH =http://somewhere.net/library.jar:...等条目。



<请注意,我不是在讨论applet或Java Web Start。想想可以使用不同后端的应用程序(例如,MySQL,Oracle),我想根据用户的后端首选项在shell脚本中准备类路径,并让类加载器下载所需的来自分发服务器的jar(本例中为jdbc驱动程序)。我也不是在谈论Maven(用户只是得到二进制分发,我不想强​​迫他们从源代码构建他们需要的东西)。

解决方案

SystemClassLoader URLClassLoader 。您可以尝试,我留给您:

 方法方法= 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);

请告诉我:))


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

解决方案

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

这篇关于类路径中的远程jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆