无法从目录加载类。与罐子同时可以在罐子解压缩/解压时加载类
社区!如果您能帮助我解决我的问题,那就太好了。
我有一个自定义类加载器,它将是 java.system.class.loader - 它包含 urls 哪里可以找到课程。像这样:
public class TestSystemClassLoader extends URLClassLoader {
public TestSystemClassLoader(ClassLoader parent) {
super(classpath(), parent);
}
private static URL[] classpath() {
try {
// I got junit-4.8.2.jar under this url.
URL url = new File("D:\\Work\\lib\\junit-4\\").toURI().toURL();
return new URL[] { url };
}
catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
}
然后我用 -Djava.system.class.loader=TestSystemClassLoader eg.TestMain 运行 java(JDK6), 其中eg.TestMain' main:
public static void main(String[] args) throws Exception {
// here I got system CL which is what I want.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// here I got: "Exception in thread "main" java.lang.ClassNotFoundException: org.junit.runners.JUnit4"
Class<?> clazz = Class.forName("org.junit.runners.JUnit4", true, cl);
}
让我生气的是,如果我解压/解压缩/解压 junit-4.8.2.jar - 那么 例如,TestMain 可以工作!
问题是 - 如何告诉 java(JDK6) 我希望整个目录位于类路径中, 即驻留在目录中的任何文件。
提前致谢!
community! Would be great if u could help me w/ my issue.
I got a custom class loader which is gonna to be java.system.class.loader
- it holds
urls where to find classes. Like this:
public class TestSystemClassLoader extends URLClassLoader {
public TestSystemClassLoader(ClassLoader parent) {
super(classpath(), parent);
}
private static URL[] classpath() {
try {
// I got junit-4.8.2.jar under this url.
URL url = new File("D:\\Work\\lib\\junit-4\\").toURI().toURL();
return new URL[] { url };
}
catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
}
Then I run the java(JDK6) with -Djava.system.class.loader=TestSystemClassLoader eg.TestMain,
where eg.TestMain' main:
public static void main(String[] args) throws Exception {
// here I got system CL which is what I want.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// here I got: "Exception in thread "main" java.lang.ClassNotFoundException: org.junit.runners.JUnit4"
Class<?> clazz = Class.forName("org.junit.runners.JUnit4", true, cl);
}
The thing which makes me mad is that if I unpack/unzip/unjar the junit-4.8.2.jar - then
eg.TestMain would work!
The question is - how to tell java(JDK6) that I want whole directory to be in classpath,
i.e. any file residing in the directory.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到所有罐子并添加它们:
Find all the jars and add them: