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

Rog*_*ach 3 java urlclassloader

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

zen*_*ngr 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);
Run Code Online (Sandbox Code Playgroud)

来源:我应该如何在运行时动态加载Jars?