一次NoClassDefFoundError排查记录

问题

cluster 模式提交 StartUp (shade)包到 spark 集群执行:

首先报错:

java.lang.NoClassDefFoundError: Could not initialize class XXX

分析

在类 XXX 初始化的时候出了问题,而类中有几个静态变量

/* other static final variable */
private static final Set<Object> ASYNC_OBJECT  = Sets.newConcurrentHashSet();

于是,就把这些静态常量的初始化工作放到一个static代码块中,并尝试捕捉其异常:

public class LockDataManager {

    /* other static final variable */
    private static final Set<Object> ASYNC_OBJECT;

    static {
        try {
            /* other initial */
            ASYNC_OBJECT = Sets.newConcurrentHashSet();
        } catch (Throwable throwable) {
            LoggerUtils.info("init error at LockDataManager.class");
            LoggerUtils.error(throwable);
            throw throwable;
        }
    }
    /* ... */
}

重新打包,提交并运行,查看日志:

java.lang.NoSuchMethodError: com.google.common.collect.Sets.newConcurrentHashSet()Ljava/util/Set;

发现了NoClassDefFoundError 是由于 NoSuchMethodError引起的,而这个方法(com.google.common.collect.Sets.newConcurrentHashSet()Ljava/util/Set;)是新版本增加的旧版本没有 。于是怀疑集群环境中可能有更低版本的 guava 包,该版本包中没有 newConcurrentHashSet 方法。去集群看:

[hadoop@bigdata0 spark-3.0.0-bin-hadoop3.2]$ ll jars/ | grep guava
-rw-r--r-- 1 hadoop hadoop  2189117 6月   6 2020 guava-14.0.1.jar

问题确定了,但是还需要再验证下:在启动任务的主方法中加入以下代码验证:

String LOCATION = ""; 
String URLLOCATION = ""; 
try {
		LOCATION = Sets.class.getProtectionDomain().getCodeSource().getLocation().getFile(); 
		URLLOCATION = URLDecoder.decode(LOCATION, "UTF-8"); 
} catch (UnsupportedEncodingException e) { 
		LoggerUtils.error(Main.class, e); 
} 
LoggerUtils.info("***loc=" + LOCATION + "\\nURLLoc=" + URLLOCATION);

再次打包,提交并运行,查看日志:

***loc=/opt/software/spark-3.0.0-bin-hadoop3.2/jars/guava-14.0.1.jar
URLLoc=/opt/software/spark-3.0.0-bin-hadoop3.2/jars/guava-14.0.1.jar

于是,找到问题根源并验证了。

小结

出现NoClassDefFoundErrorNoSuchMethodError等错误是因为 在编译时找到了对应的类(或方法等内容),但运行时没找到。其多半是因为环境中有重复的类(类全限定类名相同)

  • 比如加载了两个不同版本的jar包,而二则不兼容:代码中使用了高版本jar包中的类中的方法,但是运行时加载到了低版本的类中的方法。就会导致找不到方法的异常。

解决方法:保证环境中类(或 jar)的唯一性

  • 对于maven依赖,如果两个模块实在需要依赖不同版本的jar。那么可以将其中一个打成shade包,并relocation(重命名)package。

拓展

ClassNotFoundException 与 NoClassDefFoundError 的区别:

  • ClassNotFoundException:顾名思义,使用类加载器就加载某个类的时候找不到类;即从引导类路径,扩展类路径到当前的classpath下全部没有找到,就会抛出该异常。
  • NoClassDefFoundError :编译时存在某个类,但是运行时却找不到;或者初始化某个类的时候失败了。

ClassNotFoundException:

在使用以下方法找不到类的时候就会抛出 ClassNotFoundException

  • Class.forName()
  • ClassLoader.loadClass()
  • ClassLoader.findSystemClass()

NoClassDefFoundError:

初始化某个类的时候失败案例:

public class TempTest {
    public static void main(String[] args) {
        try {
			// 第一次调用静态方法,初始化类出错,抛 ExceptionInInitializerError
            NoClassDefFoundErrorDemo.f();
        } catch (ExceptionInInitializerError e) {
            e.printStackTrace();
        }
		// 类没有初始化成功,调用时,抛 NoClassDefFoundError 
        NoClassDefFoundErrorDemo.f();
    }

    static class NoClassDefFoundErrorDemo {
				// 初始化时出错
        private static double val = 1 / 0;
        public static void f() {}
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非理性地界生物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值