项目爆出 NoClassDefFoundError 有很大部分情况是因为依赖版本冲突导致
有的时候我们在本地编译和运行项目不会报错,但发布到线上环境就会有问题,例如maven环境又引入了本地jar包就容易出现问题
这个时候我们可以通过 打印当前运行环境 ClassLoader 加载了哪些Jar文件来排查问题
代码如下:
URL[] urLs = Launcher.getBootstrapClassPath().getURLs();
System.out.println("启动类加载器加载的jar:");
for (URL urL : urLs) {
System.out.println("==> " + urL.toExternalForm());
}
printClassLoader("扩展类加载器和其加载的jar:",this.getClass().getClassLoader().getParent());
printClassLoader("应用类加载器和其加载的jar:",this.getClass().getClassLoader());
工具类:
public static void printClassLoader(String name, ClassLoader CL) {
if (CL != null) {
System.out.println(name + " ClassLoader -> " + CL.toString());
printURLForClassLoader(CL);
} else {
System.out.println(name + " ClassLoader -> null");
}
}
public static void printURLForClassLoader(ClassLoader CL) {
Object ucp = insightField(CL, "ucp");
Object path = insightField(ucp, "path");
ArrayList ps = (ArrayList) path;
for (Object p : ps) {
System.out.println(" -=>" + p.toString());
}
}
private static Object insightField(Object obj, String fName) {
try {
Field f = null;
if (obj instanceof URLClassLoader) {
f = URLClassLoader.class.getDeclaredField(fName);
} else {
f = obj.getClass().getDeclaredField(fName);
}
f.setAccessible(true);
return f.get(obj);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
输出内容如下:=>file:/C:/Users/93340/.m2/repository/org/apache/xmlgraphics/xmlgraphics-commons/2.6/xmlgraphics-commons-2.6.jar
-=>file:/C:/Users/93340/.m2/repository/org/apache/xmlgraphics/batik-bridge/1.14/batik-bridge-1.14.jar
-=>file:/C:/Users/93340/.m2/repository/org/apache/xmlgraphics/batik-script/1.14/batik-script-1.14.jar
-=>file:/C:/Users/93340/.m2/repository/org/apache/xmlgraphics/batik-dom/1.14/batik-dom-1.14.jar
-=>file:/C:/Users/93340/.m2/repository/org/apache/xmlgraphics/batik-gvt/1.14/batik-gvt-1.14.jar