如何在运行时动态地将外部jar文件添加到ClassPath? [英] How To add an External jar File to the ClassPath Dynamically at runtime?
本文介绍了如何在运行时动态地将外部jar文件添加到ClassPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用java代码动态地将jar文件添加到项目的类路径中,如果可能的话,我想使用外部jar文件并加载它们的类,然后将它们作为Beans执行(Spring框架)。
I want to add a jar File to my project's classpath dynamically using java code if it is possible , I want to use external jar files and load their classes the execute them as Beans later (Spring framework).
谢谢:)
推荐答案
URLClassLoader child = new URLClassLoader (myJar.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);
资料来源: https://stackoverflow.com/a/60775/1360074
这篇关于如何在运行时动态地将外部jar文件添加到ClassPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文