从单独的jar调用.asSubclass时引发ClassCastException [英] ClassCastException thrown when calling .asSubclass from separate jar
问题描述
我有一个程序,可以通过它完成以下三个课程.前两个在jar1.jar
中:
I have a program through which I have the following three classes. These first two are in jar1.jar
:
Main
(使用在此处找到的jar加载技巧):
Main
(uses the jar loading trick found here):
package prob1;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
static List<Class<?>> classes = new ArrayList<Class<?>>();
static String pathToJar = "/Users/vtcakavsmoace/Desktop/jar2.jar";
public static void main(String[] args) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException {
loadJar();
classes.get(0).asSubclass(A.class).newInstance().someMethod();
}
private static void loadJar() throws IOException, ClassNotFoundException {
JarFile jarFile = new JarFile(pathToJar);
Enumeration<JarEntry> e = jarFile.entries();
URL[] urls = { new URL("jar:file:" + pathToJar + "!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);
while (e.hasMoreElements()) {
JarEntry je = e.nextElement();
if (je.isDirectory() || !je.getName().endsWith(".class")) {
continue;
}
String className = je.getName().substring(0, je.getName().length() - 6);
className = className.replace('/', '.');
classes.add(cl.loadClass(className));
}
jarFile.close();
}
}
A
:
package prob1;
public interface A {
void someMethod();
}
以及在jar2.jar
,B
中找到的第二个类:
And a second class found in jar2.jar
, B
:
package prob2;
import prob1.A;
public class B implements A {
@Override
public void someMethod() {
System.out.println("Hello, world!");
}
}
如您所见,类B
显然实现了接口A
.但是,在加载类B
并尝试在其上调用asSubclass
时,失败,并带有以下异常:
As you can see, class B
obviously implements interface A
. However, when loading class B
, and then attempting to call asSubclass
on it, fails with the following exception:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.ClassCastException: class prob2.B
at java.lang.Class.asSubclass(Class.java:3404)
at prob1.Main.main(Main.java:20)
... 5 more
请注意,通过Eclipse运行时这不会失败.
Note that this does not fail when run through Eclipse.
我在这里做错了什么?我该如何解决?
What am I doing wrong here? How can I fix this?
推荐答案
好吧,使用 Erwin Bolwidt的知识找到了答案给了我;在Main
类中,替换为:
Okay, found the answer using the knowledge that Erwin Bolwidt gave me; in class Main
, replacing:
URLClassLoader cl = URLClassLoader.newInstance(urls);
具有:
URLClassLoader cl = URLClassLoader.newInstance(urls, Main.class.getClassLoader());
解决此问题. :P
这篇关于从单独的jar调用.asSubclass时引发ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!