简体   繁体   English

用户URLClassLoader可以“即时”加载jar文件

[英]User URLClassLoader to load jar file “on the fly”

Ok, basically, I try to use the method described here JarFileLoader to load a jar containing a class that will be used the same as if it was on the classpath (the class name will be dynamic so that we can just add any jar with any class and the program will load it through parsing a text file, in the main line). 好的,基本上,我尝试使用此处描述的方法JarFileLoader加载一个包含与将在类路径上使用的类相同的类的jar(类名将是动态的,因此我们可以将任何具有任何类,程序将通过在主行中解析文本文件来加载它)。

Problem is that when I debug and check the URLClassLoader object 问题是当我调试并检查URLClassLoader对象时

protected Class<?> findClass(final String name)

Line : 线:

Resource res = ucp.getResource(path, false);

the getResource() does not find the class name in parameter. getResource()在参数中找不到类名。

Does someone already try loading a jar file this way ? 有人已经尝试过以这种方式加载jar文件吗?

Thanks. 谢谢。

Loader : 装载机

public class JarFileLoader extends URLClassLoader {
    public JarFileLoader() {
        super(new URL[] {});
    }

    public JarFileLoader withFile(String jarFile) {
        return withFile(new File(jarFile));
    }

    public JarFileLoader withFile(File jarFile) {
        try {
            if (jarFile.exists())
                addURL(new URL("file://" + jarFile.getAbsolutePath() + "!/"));
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(e);
        }
        return this;
    }

    public JarFileLoader withLibDir(String path) {
        Stream.of(new File(path).listFiles(f -> f.getName().endsWith(".jar"))).forEach(this::withFile);
        return this;
    }
}

Main : 主要:

public static void main(String[] args) {
    new Initializer();
    JarFileLoader cl = new JarFileLoader();
    cl = cl.withFile(new File("libs/dpr-common.jar"));
    try {
        cl.loadClass("com.*****.atm.dpr.common.util.DPRConfigurationLoader");
        System.out.println("Success!");
    } catch (ClassNotFoundException e) {
        System.out.println("Failed.");
        e.printStackTrace();
    } finally {
        try {
            cl.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here the test class I used. 这是我使用的测试类。 When I debug URLClassLoader I can see in the third loop the path of the jar file(loop on the classpath and the URL you add here), but still does not find ressource (and cannot debug the class URLClassPath so do not know what getRessource does exactly). 当我调试URLClassLoader时,我可以在第三个循环中看到jar文件的路径(类路径和您在此处添加的URL上的循环),但是仍然找不到资源(并且无法调试类URLClassPath,所以不知道getRessource做什么)究竟)。

Ok I take the answer from this question : How to load all the jars from a directory dynamically? 好吧,我从以下问题中得到答案: 如何从目录动态加载所有jar?

And changing the URL part at the beginning with the way it is done in the long part it works. 并从一开始就更改URL的一部分,直到完成它的大部分工作为止。

So an example could be : 因此,一个示例可能是:

String path = "libs/dpr-common.jar";
if (new File(path).exists()) {
    URL myJarFile = new File(path).toURI().toURL();
    URL[] urls = { myJarFile };
    URLClassLoader child = new URLClassLoader(urls);
    Class DPRConfLoad = Class.forName("com.thales.atm.dpr.common.util.DPRConfigurationLoader", true, child);
    Method method = DPRConfLoad.getDeclaredMethod("getInstance");
    final Object dprConf = method.invoke(DPRConfLoad);
}

All my time wasted in search while it was the example which was wrong... Still does not understand why they use a stupid URL like "jar:file..." etc. 我所有的时间都浪费在搜索上,而这是一个错误的例子...仍然不明白为什么他们使用愚蠢的URL,例如“ jar:file ...”等。

Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM