[JavaDebug (12)] NoSuchMethodError, use arthas tool to find jar package, maven command to search jar package

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. BUG description

        The essence is a NoSuchMethodError, as shown below:

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationAwareOrderComparator.sort(Ljava/util/List;)V
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:383)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:249)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:225)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com.example.demoNoSuchMethodError.DemoNoSuchMethodErrorApplication.main(DemoNoSuchMethodErrorApplication.java:13)

       This exception means that the AnnotationAwareOrderComparator class lacks the sort(Ljava/util/List;)V function, and the following function uses the function representation in the JVM.


two. Solutions

(I. Overview

       A certain class of a certain package lacks a certain function, which does not seem to be our problem. It may be caused by the version of a certain package that causes this class of this package to not have this function, or it may be that the priority of this package is higher than the others of the same name. Package, causing other packages to be overwritten, so we need to find the location of this package.

(Two) arthas tools

       So, how do you find which package this class is imported from? Using Alibaba's open source arthas tool to solve the problem, it can solve the following problems:

       1. From which jar package is this class loaded? Why are various types of related Exception reported?

       2. Why is the code I changed not executed? Could it be that I didn't commit? Wrong branch?

       3. I can’t debug online if I encounter a problem, can I only republish it by adding a log?

       4. There is a problem with the data processing of a certain user online, but it cannot be debugged online, and it cannot be reproduced offline!

       5. Is there a global perspective to view the operating status of the system?

       6. Is there any way to monitor the real-time running status of the JVM?

       Use this tool to check which package this class is imported from, but first of all, we must ensure that the process where the abnormal class is still executing when using the tool, which requires us to use try- in the code block that may be abnormal. The catch statement block is used to catch the exception. The Throwable class object is used here instead of the Exception class object to catch. It may be because if the Exception class is used, since the error is an Error, it cannot catch the Error, so it will directly report an error and exit. , And the Throwable class is the parent class of the Error class and the Exception class. It contains the Error class. Therefore, you can catch this Error, enter the catch code block, execute the blocking statement, and keep the process running, so that the arthas tool can find it when the process is running. An error occurred.

       After downloading the jar package of arthas, create a new folder and put the jar package into it. After that, you can use it in IDEA's console or in CMD. Enter the folder and enter

java -jar arthas-boot.jar 

       arthas will scan the process started by the JVM, we can select the corresponding process, enter the process, and execute the sc command to query

sc -d org.springframework.core.annotation.AnnotationAwareOrderComparator

       You can see the output as shown below, and the spring-2.5.6jar package has been found:

$ sc -d org.springframework.core.annotation.AnnotationAwareOrderComparator
 class-info        org.springframework.core.annotation.AnnotationAwareOrderComparator
 code-source       /Users/hengyunabc/.m2/repository/org/springframework/spring/2.5.6.SEC03/spring-2.5.6.SEC03.jar
 name              org.springframework.core.annotation.AnnotationAwareOrderComparator
 isInterface       false
 isAnnotation      false
 isEnum            false
 isAnonymousClass  false
 isArray           false
 isLocalClass      false
 isMemberClass     false
 isPrimitive       false
 isSynthetic       false
 simple-name       AnnotationAwareOrderComparator
 modifier          public
 annotation
 interfaces
 super-class       +-org.springframework.core.OrderComparator
                     +-java.lang.Object
 class-loader      +-sun.misc.Launcher$AppClassLoader@5c647e05
                     +-sun.misc.Launcher$ExtClassLoader@689e3d07
 classLoaderHash   5c647e05

Affect(row-cnt:1) cost in 41 ms.

       Use the jad command to view the decompiled AnnotationAwareOrderComparator source code:

    $ jad org.springframework.core.annotation.AnnotationAwareOrderComparator

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@5c647e05
  +-sun.misc.Launcher$ExtClassLoader@689e3d07

Location:
/Users/hengyunabc/.m2/repository/org/springframework/spring/2.5.6.SEC03/spring-2.5.6.SEC03.jar

/*
 * Decompiled with CFR 0_132.
 */
package org.springframework.core.annotation;

import java.lang.annotation.Annotation;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

public class AnnotationAwareOrderComparator
extends OrderComparator {
    
    
    protected int getOrder(Object obj) {
    
    
        Order order;
        if (obj instanceof Ordered) {
    
    
            return ((Ordered)obj).getOrder();
        }
        if (obj != null && (order = obj.getClass().getAnnotation(Order.class)) != null) {
    
    
            return order.value();
        }
        return Integer.MAX_VALUE;
    }
}

Affect(row-cnt:1) cost in 286 ms.

       I found that there is indeed no sort function, and finally found the problem, as long as the jar package is excluded.


(Three). maven command

       How to find the spring jar package? First of all, start with the Maven command and view the dependency tree of maven. There is no dependency conflict. You can use CTRL + F to search for the jar package. You can also directly use the maven command to search the dependency tree of the specified package, such as searching the dependency tree of the spring package:

mvn dependency:tree -Dincludes=org/springframework/spring

       This is too much trouble, then find a maven plug-in maven-helper, directly in File -> setting -> Plugins -> search for maven helper, then click install, restart IDEA.

       After the success of Maven Helper, there will be an additional tab page Dependency Analyzer in the lower left corner, through which you can perform the following operations:

       1. Conflicts (view conflicts)

       2. All Dependencies as List (view all dependencies in list form)

       3. All Dependencies as Tree (view all dependencies in tree form)

       Therefore, if the location of the spring package is found in a newly added package, then directly use the following method in the coordinate import of the package to exclude the spring dependency:

<!--        Canal客户端-->
<dependency>
    <groupId>com.alibaba.otter</groupId>
    <artifactId>canal.client</artifactId>
    <version>1.0.24</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>

       At this point, the problem is solved! ! !

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113663107