How to load JAR files dynamically at Runtime?

前端 未结 20 3688
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 05:15

Why is it so hard to do this in Java? If you want to have any kind of module system you need to be able to load JAR files dynamically. I\'m told there\'s a way of doing it b

20条回答
  •  耶瑟儿~
    2020-11-21 05:58

    How about the JCL class loader framework? I have to admit, I haven't used it, but it looks promising.

    Usage example:

    JarClassLoader jcl = new JarClassLoader();
    jcl.add("myjar.jar"); // Load jar file  
    jcl.add(new URL("http://myserver.com/myjar.jar")); // Load jar from a URL
    jcl.add(new FileInputStream("myotherjar.jar")); // Load jar file from stream
    jcl.add("myclassfolder/"); // Load class folder  
    jcl.add("myjarlib/"); // Recursively load all jar files in the folder/sub-folder(s)
    
    JclObjectFactory factory = JclObjectFactory.getInstance();
    // Create object of loaded class  
    Object obj = factory.create(jcl, "mypackage.MyClass");
    

提交回复
热议问题