Java自定义ClassLoader加载jar

需要两步:

1、构建所有需要加载的jar路径,比如 new URL("file:d:/xxxx/1.jar")

2、新建 URLClassLoader 加载 jars

打包一个 1.jar,其中有一个类com.maxzhao.CatPrint

1
2
3
4
5
6
7
public class ClassLoaderTest {
public void tests(){
URL[] pluginLibsPath = new URL[]{new URL("file:d:/xxxx/1.jar")};
ClassLoader classLoader = new URLClassLoader(pluginLibsPath, Thread.currentThread().getContextClassLoader());
Class catClass = classLoader.loadClass(com.maxzhao.CatPrint);
}
}

本文地址: https://github.com/maxzhao-it/blog/post/cb80c3ad/