Thread.currentThread().setContextClassLoader 似乎无法正常工作
我尝试创建一个 jar,使用它进行编译,但不发送该 jar。我希望用户提供该 jar 并将我的应用程序指向该 jar。
想象一下以下内容:
这是我的源代码树:
main
Main.java
test
Test.java
我创建了两个 jar:
main.jar
main
Main.class
test.jar
test
Test.class
我的源代码如下所示:
Main.java
package main;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import test.Test;
public class Main {
public static void main(String[] args) throws Exception {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { new File("test.jar").toURI().toURL() }, null);
Thread.currentThread().setContextClassLoader(urlClassLoader);
Test.test();
}
}
Test.java
package test;
public class Test {
public static void test() {
System.out.println("Hello world!");
}
}
我将 main.jar 称为 main.jar 但类路径上没有 test.jar。我希望 URLClassLoader 加载 test.jar 并让程序打印 Hello world!。
但似乎没有使用我的 URLClassLoader 而是使用系统类加载器。我得到:
Exception in thread "main" java.lang.NoClassDefFoundError: test/Test
at main.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: test.Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
我的错误是什么?怎样做才正确呢?
I try to create a jar, use it for compiling but do not ship the jar. I expect the user to provide the jar and pointing my application to this jar.
Imagine the following:
This is my source tree:
main
Main.java
test
Test.java
I create two jars:
main.jar
main
Main.class
test.jar
test
Test.class
My source looks like this:
Main.java
package main;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import test.Test;
public class Main {
public static void main(String[] args) throws Exception {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { new File("test.jar").toURI().toURL() }, null);
Thread.currentThread().setContextClassLoader(urlClassLoader);
Test.test();
}
}
Test.java
package test;
public class Test {
public static void test() {
System.out.println("Hello world!");
}
}
I call the main.jar but without test.jar on the classpath. I would expect the URLClassLoader to load the test.jar and the program to print Hello world!.
But it seems not to use my URLClassLoader but the system classloader instead. I get:
Exception in thread "main" java.lang.NoClassDefFoundError: test/Test
at main.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: test.Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
What is my mistake? How to do it right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于那些寻找答案的人,请查看 如何更改 CLASSPATH在 Java 中? 获取解决方法和更多解释
For those looking for an answer have a look at How do you change the CLASSPATH within Java? for a workaround and more explanations