是否可以在运行时加载库?

时间:2010-11-06 05:34:47

标签: java urlclassloader

有没有办法在运行时加载java库(.jar文件),如果它不在类路径上?

1 个答案:

答案 0 :(得分:6)

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

来源:How should I load Jars dynamically at runtime?