如何动态加载目录中的所有jar? [英] How to load all the jars from a directory dynamically?

查看:145
本文介绍了如何动态加载目录中的所有jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在创建一个插件,需要动态加载jar并访问这些jar的类和方法。我尝试使用URLClassLoader并能够加载类,如下所示

Hi I am creating a plugin which requires loading jars dynamically and accessing the classes and methods of these jars. I tried using URLClassLoader and am able to load the classes as shown below

URL myJarFile = new URL("jar","","file:"+jarPath);
URLClassLoader sysLoader =(URLClassLoader)ClassLoader.getSystemClassLoader();
Class sysClass = URLClassLoader.class;
Method sysMethod = sysClass.getDeclaredMethod("addURL", new Class[]{URL.class});
sysMethod.setAccessible(true);
sysMethod.invoke(sysLoader, new Object[]{myJarFile});

但问题是我们必须通过单独指定其名称来将类加载到classLoader中。
我想要的是从class-path中的所有jar加载所有类,并在任何时间点访问它们。

But the issue with this is that we have to load classes into classLoader by specifying their name individually. What I want is to load all the classes from all jars in class-path and access them any point of time.

是否可以使用URLClassLoader?如果没有,那么其他选择是什么?
OSGI实现这个目的有多大帮助?

Is it possible with URLClassLoader? If not what are the other options? How much helpful is OSGI in achieving this?

提前致谢!

推荐答案

您需要先加载jar,然后从那里加载所需的类。

You need to load your jar first and then load the required class from there.

URL myJarFile = new URL("jar","","file:"+jarPath);
URLClassLoader child = new URLClassLoader (myJarFile , 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);

然后你可以先得到jar文件中所有类的列表:

Then you can get a list of all the classes in the jar file first:

List<String> classNames=new ArrayList<String>();
ZipInputStream zip=new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));
for(ZipEntry entry=zip.getNextEntry();entry!=null;entry=zip.getNextEntry())
    if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
        StringBuilder className=new StringBuilder();
        for(String part : entry.getName().split("/")) {
            if(className.length() != 0)
                className.append(".");
            className.append(part);
            if(part.endsWith(".class"))
                className.setLength(className.length()-".class".length());
        }
        classNames.add(className.toString());
    }

获得课程列表后,请执行以下操作:

Once you have list of your classes do the following:

File file = new File("Absolute Path to your jar");  
            URL url = file.toURI().toURL();  
            URL[] urls = {url};  
            ClassLoader loader = new URLClassLoader(urls);  
            Class myClass = loader.loadClass(classNames.get(0));  
            System.out.println("Executing...");  
            Object tester = myClass.newInstance(); 
        System.out.println("Test");

这篇关于如何动态加载目录中的所有jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆