Java example with ClassLoader

后端 未结 3 2034
野性不改
野性不改 2021-01-05 20:27

I have small problem. I learn java SE and find class ClassLoader. I try to use it in below code: I am trying to use URLClassLoader to dynamically load a class at runtime.

3条回答
  •  感情败类
    2021-01-05 20:50

    Class classS = urlcl.loadClass("michal.collection.Stack");
    [...]
    Object object = classS.newInstance();
    michal.collection.Stack new_name = (michal.collection.Stack) object;
    

    So you're attempting to dynamically load a class and then you statically refer to it. If you can already statically link to it, then its loaded and you can't load it again. You'll need to access the methods by reflection.

    What you would usually do is have the loaded class implement an interface from the parent class loader. After an instance is created (usually just a single instance), then you can refer to it through a reference with a type of the interface.

    public interface Stack {
       [...]
    }
    [...]
        URLClassLoader urlcl = URLClassLoader.newInstance(new URL[] {
           new URL(
               "file:///I:/Studia/PW/Sem6/_repozytorium/workspace/Test/testJavaLoader.jar"
           )
        });
        Class clazz = urlcl.loadClass("michal.collection.StackImpl");
        Class stackClass = clazz.asSubclass(Stack.class);
        Constructor ctor = stackClass.getConstructor();
        Stack stack = ctor.newInstance();
    

    (Usual Stack Overflow disclaimer about not so much as compiling.)

    You'll need to add error handling to taste. URLClassLoader.newInstance adds a bit of refinement to URLClassLoader. Class.newInstance has completely broken exception handling and should be avoided.

提交回复
热议问题