如何动态加载目录中的所有jar?

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

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加载所有类,并在任何时间点访问它们。

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

提前致谢!

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

 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文件中所有类的列表:

 List classNames=new ArrayList(); 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()); } 

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

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

Apache Felix文件安装可能正是您想要的。 它将监视指定的目录并动态加载其中的任何包。 只有捆绑包导出的包可用于类路径上的其他捆绑包,但所有类都将被加载。