Java 用户URLClassLoader加载jar文件;“在飞行中”;
好的,基本上,我尝试使用这里描述的方法加载一个jar,其中包含一个类,该类的使用方式与类路径相同(类名将是动态的,这样我们就可以添加带有任何类的任何jar,程序将通过在主线中解析文本文件来加载它) 问题是,当我调试和检查URLClassLoader对象时Java 用户URLClassLoader加载jar文件;“在飞行中”;,java,urlclassloader,Java,Urlclassloader,好的,基本上,我尝试使用这里描述的方法加载一个jar,其中包含一个类,该类的使用方式与类路径相同(类名将是动态的,这样我们就可以添加带有任何类的任何jar,程序将通过在主线中解析文本文件来加载它) 问题是,当我调试和检查URLClassLoader对象时 protected Class<?> findClass(final String name) getResource()在参数中找不到类名 有人已经尝试过用这种方式加载jar文件了吗 谢谢 加载器: public class J
protected Class<?> findClass(final String name)
getResource()在参数中找不到类名
有人已经尝试过用这种方式加载jar文件了吗
谢谢
加载器:
public class JarFileLoader extends URLClassLoader {
public JarFileLoader() {
super(new URL[] {});
}
public JarFileLoader withFile(String jarFile) {
return withFile(new File(jarFile));
}
public JarFileLoader withFile(File jarFile) {
try {
if (jarFile.exists())
addURL(new URL("file://" + jarFile.getAbsolutePath() + "!/"));
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
return this;
}
public JarFileLoader withLibDir(String path) {
Stream.of(new File(path).listFiles(f -> f.getName().endsWith(".jar"))).forEach(this::withFile);
return this;
}
}
主要内容:
public static void main(String[] args) {
new Initializer();
JarFileLoader cl = new JarFileLoader();
cl = cl.withFile(new File("libs/dpr-common.jar"));
try {
cl.loadClass("com.*****.atm.dpr.common.util.DPRConfigurationLoader");
System.out.println("Success!");
} catch (ClassNotFoundException e) {
System.out.println("Failed.");
e.printStackTrace();
} finally {
try {
cl.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这里是我使用的测试类。当我调试URLClassLoader时,我可以在第三个循环中看到jar文件的路径(类路径上的循环和您在此处添加的URL),但仍然找不到ressource(并且无法调试类URLClassPath,因此不知道getRessource到底做了什么)。好的,我从这个问题得到答案: 在开始时改变URL部分,在长部分中改变URL部分的工作方式 例如:
String path = "libs/dpr-common.jar";
if (new File(path).exists()) {
URL myJarFile = new File(path).toURI().toURL();
URL[] urls = { myJarFile };
URLClassLoader child = new URLClassLoader(urls);
Class DPRConfLoad = Class.forName("com.thales.atm.dpr.common.util.DPRConfigurationLoader", true, child);
Method method = DPRConfLoad.getDeclaredMethod("getInstance");
final Object dprConf = method.invoke(DPRConfLoad);
}
我所有的时间都浪费在搜索上,而这是一个错误的例子。。。仍然不明白为什么他们会使用一个愚蠢的URL,比如“jar:file…”等等
谢谢大家。也尝试了这个:不改变任何东西。如果您希望我们帮助您调试代码,您必须提供更多代码的详细信息。理想情况下,以Java主程序或JUnit测试的形式提供复制。它将具有依赖性,但您可以解释这些。不清楚从Jar文件加载类是否有效,或者仅从Jar文件加载资源是否会导致问题。也许你只是有一段时间在加载资源。我只是找到一个关于这个的帖子,我现在就试试。如果有效,我将结束此问题。:-)好的,简单的方法是错误的,但经过纠正后,它终于工作了!我不知道为什么示例是错误的,而用循环等解释的漫长道路是正确的。。。所以唯一要做的就是在文件上使用内部方法toURL,就这样<代码>字符串path=“libs/dpr common.jar”;if(new File(path).exists())URL myJarFile=new File(path).toURI().toul()代码>
String path = "libs/dpr-common.jar";
if (new File(path).exists()) {
URL myJarFile = new File(path).toURI().toURL();
URL[] urls = { myJarFile };
URLClassLoader child = new URLClassLoader(urls);
Class DPRConfLoad = Class.forName("com.thales.atm.dpr.common.util.DPRConfigurationLoader", true, child);
Method method = DPRConfLoad.getDeclaredMethod("getInstance");
final Object dprConf = method.invoke(DPRConfLoad);
}