我想使用 Java 代码将 JAR 文件动态添加到我的项目的类路径中。如果可能的话,我想使用外部 jar 文件并加载它们的类,然后稍后将它们作为 bean 执行(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);
关于java - 如何在运行时动态地将外部 jar 文件添加到类路径中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15204913/