Java ClassCastException是因为类加载器?

Java ClassCastException是因为类加载器?,java,jvm,classloader,scjp,Java,Jvm,Classloader,Scjp,在使用类加载器时,我遇到了以下异常: Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton 这是否意味着一个类加载器的实例不能强制转换为另一个类加载器的类 检查我的代码,因为类加载器,我能够实例化3个单例,即使使用“”安全性 public static void main(String[] args) throws Exception {

在使用类加载器时,我遇到了以下异常:

Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton
这是否意味着一个类加载器的实例不能强制转换为另一个类加载器的类

检查我的代码,因为类加载器,我能够实例化3个单例,即使使用“”安全性

public static void main(String[] args) throws Exception {
        URL basePath = new URL("file:/myMavenPath/target/classes/");

    Object instance = getClassInstance(Singleton.class);
    System.out.println(instance);
    //
    Object instance2 = getClassInstance(
            new URLClassLoader( new URL[]{basePath} , null )
                    .loadClass("my.Singleton")
    );
    System.out.println(instance2);
    //
    Object instance3 = getClassInstance(
            new URLClassLoader( new URL[]{basePath} , null )
                    .loadClass("my.Singleton")
    );
    System.out.println(instance3);

    // Only the 1st cast is ok
    Singleton testCast1 = (Singleton) instance;
    System.out.println("1st cast ok");
    Singleton testCast2 = (Singleton) instance2;
    System.out.println("2nd cast ok");
    Singleton testCast3 = (Singleton) instance3;
    System.out.println("3rd cast ok");
}

private static Object getClassInstance(Class clazz) throws Exception {
    Method method = clazz.getMethod("getInstance");
    method.setAccessible(true);
    return method.invoke(null);
}


class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    public static Singleton getInstance() {
        return INSTANCE;
    }

    private Singleton() {
        Exception e = new Exception();
        StackTraceElement[] stackTrace = e.getStackTrace();
        if (!"<clinit>".equals(stackTrace[1].getMethodName())) {
            throw new IllegalStateException("You shall not instanciate the Singleton twice !",e);
        }
    }

    public void sayHello() {
        System.out.println("Hello World ! " + this);
    }

}
publicstaticvoidmain(字符串[]args)引发异常{
URL basePath=新URL(“文件:/myMavenPath/target/classes/”;
对象实例=getClassInstance(Singleton.class);
System.out.println(实例);
//
对象实例2=getClassInstance(
新的URLClassLoader(新的URL[]{basePath},null)
.loadClass(“my.Singleton”)
);
System.out.println(实例2);
//
对象实例3=getClassInstance(
新的URLClassLoader(新的URL[]{basePath},null)
.loadClass(“my.Singleton”)
);
System.out.println(实例3);
//只有第一个演员可以
Singleton testCast1=(Singleton)实例;
系统输出打印项次(“第一次铸造正常”);
Singleton testCast2=(Singleton)instance2;
系统输出打印项次(“第二次铸造正常”);
Singleton testCast3=(Singleton)instance3;
System.out.println(“第三次播放正常”);
}
私有静态对象getClassInstance(类clazz)引发异常{
Method=clazz.getMethod(“getInstance”);
方法setAccessible(true);
返回方法.invoke(null);
}
单件阶级{
私有静态最终单例实例=新单例();
公共静态单例getInstance(){
返回实例;
}
私人单身人士(){
异常e=新异常();
StackTraceElement[]stackTrace=e.getStackTrace();
如果(!“”.equals(stackTrace[1].getMethodName()){
抛出新的IllegalStateException(“你不能两次实例化Singleton!”,e);
}
}
公共空间{
System.out.println(“你好,世界!”+这个);
}
}
是的,你是对的


这通常发生在OSGi项目中,因为依赖关系管理不好

确实如此。不能在由不同类装入器装入的类之间使用强制转换


可能会让事情更清楚…

你不能在类加载器之间转换。类标识由完全限定名和类加载器组成。检查类身份验证

java可以运行身份验证吗?