简体   繁体   English

从单独的jar调用.asSubclass时抛出ClassCastException

[英]ClassCastException thrown when calling .asSubclass from separate jar

I have a program through which I have the following three classes. 我有一个程序,通过它我有以下三个类。 These first two are in jar1.jar : 前两个是在jar1.jar

Main (uses the jar loading trick found here ): Main (使用此处的jar加载技巧):

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

package prob1;

public interface A {
    void someMethod();
}

And a second class found in jar2.jar , B : jar2.jar找到第二个类, B

package prob2;

import prob1.A;

public class B implements A {

    @Override
    public void someMethod() {
        System.out.println("Hello, world!");
    }

}

As you can see, class B obviously implements interface A . 如您所见, B类显然实现了接口A However, when loading class B , and then attempting to call asSubclass on it, fails with the following exception: 但是,当加载类B ,然后尝试在其上调用asSubclass时,将失败并出现以下异常:

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

Note that this does not fail when run through Eclipse. 请注意,在Eclipse中运行时不会失败。

What am I doing wrong here? 我在这做错了什么? How can I fix this? 我怎样才能解决这个问题?

Okay, found the answer using the knowledge that Erwin Bolwidt gave me; 好的,根据Erwin Bolwidt给我的知识找到答案; in class Main , replacing: Main类中,替换:

URLClassLoader cl = URLClassLoader.newInstance(urls);

with: 有:

URLClassLoader cl = URLClassLoader.newInstance(urls, Main.class.getClassLoader());

Fixes the problem. 解决了这个问题。 :P :P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM