How to load JAR files dynamically at Runtime?

前端 未结 20 3724
伪装坚强ぢ
伪装坚强ぢ 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:56

    please take a look at this project that i started: proxy-object lib

    This lib will load jar from file system or any other location. It will dedicate a class loader for the jar to make sure there are no library conflicts. Users will be able to create any object from the loaded jar and call any method on it. This lib was designed to load jars compiled in Java 8 from the code base that supports Java 7.

    To create an object:

        File libDir = new File("path/to/jar");
    
        ProxyCallerInterface caller = ObjectBuilder.builder()
                .setClassName("net.proxy.lib.test.LibClass")
                .setArtifact(DirArtifact.builder()
                        .withClazz(ObjectBuilderTest.class)
                        .withVersionInfo(newVersionInfo(libDir))
                        .build())
                .build();
        String version = caller.call("getLibVersion").asString();
    

    ObjectBuilder supports factory methods, calling static functions, and call back interface implementations. i will be posting more examples on the readme page.

提交回复
热议问题