How to load JAR files dynamically at Runtime?

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

    This can be a late response, I can do it as this (a simple example for fastutil-8.2.2.jar) using jhplot.Web class from DataMelt (http://jwork.org/dmelt)

    import jhplot.Web;
    Web.load("http://central.maven.org/maven2/it/unimi/dsi/fastutil/8.2.2/fastutil-8.2.2.jar"); // now you can start using this library
    

    According to the documentation, this file will be download inside "lib/user" and then dynamically loaded, so you can start immediately using classes from this jar file in the same program.

    0 讨论(0)
  • 2020-11-21 06:01

    You should take a look at OSGi, e.g. implemented in the Eclipse Platform. It does exactly that. You can install, uninstall, start and stop so called bundles, which are effectively JAR files. But it does a little more, as it offers e.g. services that can be dynamically discovered in JAR files at runtime.

    Or see the specification for the Java Module System.

    0 讨论(0)
  • 2020-11-21 06:04

    The reason it's hard is security. Classloaders are meant to be immutable; you shouldn't be able to willy-nilly add classes to it at runtime. I'm actually very surprised that works with the system classloader. Here's how you do it making your own child classloader:

    URLClassLoader child = new URLClassLoader(
            new URL[] {myJar.toURI().toURL()},
            this.getClass().getClassLoader()
    );
    Class classToLoad = Class.forName("com.MyClass", true, child);
    Method method = classToLoad.getDeclaredMethod("myMethod");
    Object instance = classToLoad.newInstance();
    Object result = method.invoke(instance);
    

    Painful, but there it is.

    0 讨论(0)
  • 2020-11-21 06:07

    I needed to load a jar file at runtime for both java 8 and java 9+ (above comments don't work for both of these versions). Here is the method to do it (using Spring Boot 1.5.2 if it may relate).

    public static synchronized void loadLibrary(java.io.File jar) {
        try {            
            java.net.URL url = jar.toURI().toURL();
            java.lang.reflect.Method method = java.net.URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{java.net.URL.class});
            method.setAccessible(true); /*promote the method to public access*/
            method.invoke(Thread.currentThread().getContextClassLoader(), new Object[]{url});
        } catch (Exception ex) {
            throw new RuntimeException("Cannot load library from jar file '" + jar.getAbsolutePath() + "'. Reason: " + ex.getMessage());
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:08

    Another version of the hackish solution from Allain, that also works on JDK 11:

    File file = ...
    URL url = file.toURI().toURL();
    URLClassLoader sysLoader = new URLClassLoader(new URL[0]);
    
    Method sysMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
    sysMethod.setAccessible(true);
    sysMethod.invoke(sysLoader, new Object[]{url});
    

    On JDK 11 it gives some deprecation warnings but serves as a temporary solution those who use Allain solution on JDK 11.

    0 讨论(0)
  • 2020-11-21 06:11

    The following solution is hackish, as it uses reflection to bypass encapsulation, but it works flawlessly:

    File file = ...
    URL url = file.toURI().toURL();
    
    URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(classLoader, url);
    
    0 讨论(0)
提交回复
热议问题