NoSuchMethodError NoClassDefFoundError ClassNotFoundException Troubleshooting Ideas

NoClassDefFoundError

The error is thrown at runtime and appears when Tomcat starts. The scenarios are:

1. B.class is called in A.class at runtime, but B.class does not exist, then NoClassDefFoundError will be thrown;

2. Loaded class initialization error The loaded class has an error in the initialization (loaded->linked->initialized) process, and the initialization process is irreversible. NoClassDefFoundError will be thrown wherever the class is used in the future. This error usually occurs in the clinit method, which may be static variables and static code blocks. Usually the error stack appears as:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class xxxx

 

二、ClassNotFoundException

The error is thrown at runtime and the class is not found at startup. Investigation ideas:

1. Check whether there is a jar conflict

#!/bin/bash
for file in *.jar; do 
grepResult=`/opt/taobao/java/bin/jar tvf $file | grep $2`;  

if [ -n "$grepResult" ]; then
    echo $file;
    echo -e "    * "$grepResult"\n";
fi
done

--使用方式(查找当前目录下):findClassInJar.sh . com/alibaba/common/logging/Logger

If a different version of the jar appears, exclude the wrong one.

 

Comprehensive investigation cases of the first and second points:

  • step1: Does the submodule used by this class introduce a jar package?
  • step2: Does the class exist in the jar package?
  • step3: Is there a version conflict in the jar package where the class is located?
  • step4: Are there static variables in the class or static block initialization failed?

3. NoSuchMethodError

This error is usually caused by a class loading conflict, referencing a mismatched class version.

Investigation ideas:

1. Add -XX:+TraceClassLoading to the JVM startup parameters, and the Loading class information will be printed at startup (the project will explode if the project is too large)

2. Use mvn dependency:tree -Dverbose -Dincludes=groupId:artifactId to print the jar that is suspected to be loaded with problems

3. Exclude conflicting jars: <exclusion> or specify the correct version directly in the main pom

4、

grep -rl 'org.apache.commons.collections4.CollectionUtils' /home/admin/usa-console/target/usa-console/BOOT-INF/lib

Check which jars in this directory contain CollectionUtils

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325527645&siteId=291194637