Java CustomloaderDep无法强制转换为CustomloaderDep?

Java CustomloaderDep无法强制转换为CustomloaderDep?,java,classloader,Java,Classloader,我得到了java.lang.ClassCastException:CustomLoaderDependence无法转换为CustomLoaderDependence我知道原因是两个类由不同的类加载器加载(一个由customclassloader加载,另一个默认为sun.misc.Launcher$AppClassLoader) 即使在使用-Djava.system.class.loader=CustomClassLoader运行程序之后,我的自定义类加载器也不会被使用,除非我没有明确执行clas

我得到了
java.lang.ClassCastException:CustomLoaderDependence无法转换为CustomLoaderDependence
我知道原因是两个类由不同的类加载器加载(一个由
customclassloader
加载,另一个默认为
sun.misc.Launcher$AppClassLoader

即使在使用
-Djava.system.class.loader=CustomClassLoader
运行程序之后,我的自定义类加载器也不会被使用,除非我没有明确执行
class.forName(“customloaderdependence”,true,ClassLoader.getSystemClassLoader())但当我执行
新建CustomLoaderDependence()时,会使用默认的sun.misc.Launcher$AppClassLoader

这是我的自定义类加载器

   public class CustomClassLoader extends ClassLoader {


     public CustomClassLoader() {
            super();
        }
    /**
     * This constructor is used to set the parent ClassLoader
     */
    public CustomClassLoader(ClassLoader parent) {
        super(parent);
    }

    /**
     * Loads the class from the file system. The class file should be located in
     * the file system. The name should be relative to get the file location
     *
     * @param name
     *            Fully Classified name of class, for example com.journaldev.Foo
     */
    private Class getClass(String name) throws ClassNotFoundException {
        String file = name + ".class";
        byte[] b = null;
        try {
            // This loads the byte code data from the file
            b = loadClassFileData(file);
            // defineClass is inherited from the ClassLoader class
            // that converts byte array into a Class. defineClass is Final
            // so we cannot override it
            Class c = defineClass(name, b, 0, b.length);
            resolveClass(c);
            return c;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Every request for a class passes through this method. If the class is in
     * com.journaldev package, we will use this classloader or else delegate the
     * request to parent classloader.
     *
     *
     * @param name
     *            Full class name
     */
    @Override
    public Class loadClass(String name) throws ClassNotFoundException {
        System.out.println("Loading Class '" + name + "'" );
        if (name.contains("CustomloaderDependency") || name.contains("TestCustomLoader")) {
            System.out.println("Loading Class using CustomClassLoader");
            return getClass(name);
        }
        return super.loadClass(name);
    }

    /**
     * Reads the file (.class) into a byte array. The file should be
     * accessible as a resource and make sure that its not in Classpath to avoid
     * any confusion.
     *
     * @param name
     *            File name
     * @return Byte array read from the file
     * @throws IOException
     *             if any exception comes in reading the file
     */
    private byte[] loadClassFileData(String name) throws IOException {
        /*InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(
                name);*/
       // FileInputStream stream = new FileInputStream("F:\\workspaces\\test\\Test\\bin\\"+name);
        FileInputStream stream = new FileInputStream("F:\\"+name);
        int size = stream.available();
        byte buff[] = new byte[size];
        //DataInputStream in = new DataInputStream(stream);
        stream.read(buff);
        stream.close();
        return buff;
    }
}
这是我的测试课

我用
-Djava.system.class.loader=CustomClassLoader
运行这个程序,但看起来默认的类加载器仍然用于我试图用其进行向下转换的类

    public class TestCustomLoader {

            public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

            Class classObject = Class.forName("CustomloaderDependency", true, ClassLoader.getSystemClassLoader() );
            Object customloaderDependency = classObject.newInstance();

            System.out.println("classloader from my custom is "+customloaderDependency.getClass().getClassLoader());
           // above prints CustomClassLoader@15db9742

            System.out.println("classloader for casting is "+CustomloaderDependency.class.getClassLoader());
           // above prints  sun.misc.Launcher$AppClassLoader@14dad5dc

            CustomloaderDependency finalObject = (CustomloaderDependency)customloaderDependency;
          //above line i am getting exception


        }
我是通过工作得到的

  @Override
    public Class loadClass(String name) throws ClassNotFoundException {
        System.out.println("Loading Class '" + name + "'" );
        if (name.contains("CustomloaderDependency") || name.contains("TestCustomLoader")) {
            System.out.println("Loading Class using CustomClassLoader");
            return getClass(name);
        }
        return super.loadClass(name);
    }
在上述修复之前,调用我的customclassloader来加载
TestCustomLoader
,但我是通过
Launcher$AppClassLoader
加载它的,因为
if(name.contains(“CustomLoaderDependence”){


因此,它依赖于所有的类(如静态提到的类,即括号中的单词)您已经实例化了一个由自定义类加载器加载的类。在主程序中提到该类可以让它由系统类加载器加载。您显然知道错误消息背后的原因。我不明白您有什么问题。您能告诉我吗e详细信息?我在
CustomloaderDependency finalObject=(CustomloaderDependency)获得类强制转换异常customloaderDependency;
这意味着customloaderDependency对象由customclassloader加载,但我正在下载的customloaderDependency是由`sun.misc.Launcher$AppClassLoader`是的,静态提到的类(cast中的单词)由已加载主类及其所有依赖项的系统类加载器加载,而变量
customLoaderDependence
引用的对象类由自定义类加载器加载。但系统类加载器是我的自定义类加载器,因为我正在使用
Djava.system.class.loader=Cust运行pragramomClassLoader
。因此,即使是静态提到的类(论文中的单词)和主类也不应该由customclassloader加载吗?嗯……你是对的。但对我来说,它可以工作:使用你的设置(带有控制台输出),我首先从自定义类加载器中获得三行输出。因此它被使用并且可以工作。您肯定还有其他问题。。。