如何在JDK11+中动态加载运行时的JAR文件?

How to load JAR files dynamically at Runtime in JDK11+?(如何在JDK11+中动态加载运行时的JAR文件?)

本文介绍了如何在JDK11+中动态加载运行时的JAR文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用以下解决方案在运行时动态加载JAR文件:

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);

这是使用How to load JAR files dynamically at Runtime?

的答案完成的

我现在想要一个适用于JDK11+的解决方案,与我使用的原始解决方案等价。因此,无需第三方库/框架或加载/调用单个类即可编程完成。

我尝试了以下操作:

  1. 创建了扩展UrlClassLoader的DynamicClassLoader:
public final class DynamicClassLoader extends URLClassLoader {


    public DynamicClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }

    public DynamicClassLoader(String name, ClassLoader parent) {
        super(name, new URL[0], parent);
    }
    
    public DynamicClassLoader(ClassLoader parent) {
        this("classpath", parent);
    }

    public void addURL(URL url) {
        super.addURL(url);
    }
    
}
  1. 然后我使用java.system.class.loader标志启动我的应用程序:

java -Djava.system.class.loader=com.example.DynamicClassLoader

然后我有一个JAR文件作为路径对象,我使用以下方法调用它:

public void loadJar(Path path) throws Exception {
        
        URL url = path.toUri().toURL();
        
        DynamicClassLoader classLoader = (DynamicClassLoader)ClassLoader.getSystemClassLoader();
        Method method = DynamicClassLoader.class.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(classLoader, url);        
    }

调用此方法时,我得到以下强制转换类异常:

class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class com.example.classloader.DynamicClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader is
 in module java.base of loader 'bootstrap'; com.example.classloader.DynamicClassLoader is in unnamed module of loader 'app')

我在OpenJDK11(内部版本号11.0.10+9)上使用Spring Boot(2.4)。

推荐答案

根据评论区中的讨论,我找到了解决方案。这可能不像我希望的那样通用,并假设您知道要使用的类(相反,只需将Maven依赖项添加到pom.xml或旧的JDK8解决方案中)。

  1. 下载Maven依赖项(使用Jeka)

List<Path> paths = resolveDependency(groupId, artifactId, version);

public List<Path> resolveDependency(String groupId, String artifactId, String version) throws Exception {
        
        String dependency = groupId + ":" + artifactId + ":" + version;
        
        JkDependencySet deps = JkDependencySet.of()
                .and(dependency)
                .withDefaultScopes(COMPILE_AND_RUNTIME);

        JkDependencyResolver resolver = JkDependencyResolver.of(JkRepo.ofMavenCentral());
        List<Path> paths = resolver.resolve(deps, RUNTIME).getFiles().getEntries();

        return paths;

    }
  1. 加载JAR文件

List<Class> classes = loadDependency(paths);

public List<Class> loadDependency(List<Path> paths) throws Exception {

        List<Class> classes = new ArrayList<>();

        for(Path path: paths){

            URL url = path.toUri().toURL();
            URLClassLoader child = new URLClassLoader(new URL[] {url}, this.getClass().getClassLoader());

            ArrayList<String> classNames = getClassNamesFromJar(path.toString());

            for (String className : classNames) {
                Class classToLoad = Class.forName(className, true, child);
                classes.add(classToLoad);
            }
        }

        return classes;

    }


    // Returns an arraylist of class names in a JarInputStream
    private ArrayList<String> getClassNamesFromJar(JarInputStream jarFile) throws Exception {
        ArrayList<String> classNames = new ArrayList<>();
        try {
            //JarInputStream jarFile = new JarInputStream(jarFileStream);
            JarEntry jar;

            //Iterate through the contents of the jar file
            while (true) {
                jar = jarFile.getNextJarEntry();
                if (jar == null) {
                    break;
                }
                //Pick file that has the extension of .class
                if ((jar.getName().endsWith(".class"))) {
                    String className = jar.getName().replaceAll("/", "\.");
                    String myClass = className.substring(0, className.lastIndexOf('.'));
                    classNames.add(myClass);
                }
            }
        } catch (Exception e) {
            throw new Exception("Error while getting class names from jar", e);
        }
        return classNames;
    }


// Returns an arraylist of class names in a JarInputStream
// Calls the above function by converting the jar path to a stream
private ArrayList<String> getClassNamesFromJar(String jarPath) throws Exception {
        return getClassNamesFromJar(new JarInputStream(new FileInputStream(jarPath)));
    }

  1. 使用类

就像雷纳托指出的那样,要知道您需要了解使用它的类。在我的例子中,它是一个骆驼组件,我需要强制转换并添加到这个框架中。类是您在第二步中检索到的类,方案是component的名称。


Component camelComponent = getComponent(classes, scheme);
context.addComponent(scheme, camelComponent);

public Component getComponent(List<Class> classes, String scheme) throws Exception {

        Component component = null;
        for(Class classToLoad: classes){
            String className = classToLoad.getName().toLowerCase();
            if(className.endsWith(scheme + "component")){
                Object object =  classToLoad.newInstance();
                component = (Component) object;
            }
        }

        return component;
    }

因此,第二部分是使其动态可用。添加了第一部分和第三部分,以获得完整解决方案的示例。

这篇关于如何在JDK11+中动态加载运行时的JAR文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在JDK11+中动态加载运行时的JAR文件?