内容简介:有时spring boot应用会遇到Demo:很多时候当应用抛出异常后,进程退出了,就比较难排查问题。可以先改下main函数,把异常catch住:
前言
有时spring boot应用会遇到 java.lang.NoSuchMethodError
的问题,下面以具体的demo来说明怎样利用 arthas
来排查。
Demo: https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-NoSuchMethodError
在应用的main函数里catch住异常,保证进程不退出
很多时候当应用抛出异常后,进程退出了,就比较难排查问题。可以先改下main函数,把异常catch住:
public static void main(String[] args) throws IOException {
try {
SpringApplication.run(DemoNoSuchMethodErrorApplication.class, args);
} catch (Throwable e) {
e.printStackTrace();
}
// block
System.in.read();
}
Demo启动之后,抛出的异常是:
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)
显然,异常的意思是 AnnotationAwareOrderComparator
缺少 sort(Ljava/util/List;)V
这个函数。
安装arthas
参考: https://alibaba.github.io/arthas/install-detail.html
使用sc命令查找类所在的jar包
应用需要抛出了异常,但是进程还没有退出,我们用arthas来attach上去。比如在mac下面:
./as.sh
然后选择 com.example.demoNoSuchMethodError.DemoNoSuchMethodErrorApplication
进程。
再执行 sc 命令来查找类:
$ 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.
可以看到 AnnotationAwareOrderComparator
是从 spring-2.5.6.SEC03.jar
里加载的。
使用jad查看反编绎的源代码
下面使用 jad
命令来查看 AnnotationAwareOrderComparator
的源代码
$ 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.
可见, AnnotationAwareOrderComparator
的确没有 sort(Ljava/util/List;)V
函数。
排掉依赖,解决问题
从上面的排查里,可以确定
-
AnnotationAwareOrderComparator来自spring-2.5.6.SEC03.jar,的确没有sort(Ljava/util/List;)V函数。
所以,可以检查maven依赖,把spring 2的jar包排掉,这样子就可以解决问题了。
总结
NoSuchMethodError
链接
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- TCP 重传问题排查思路与实践
- 当Dubbo遇上Arthas:排查问题的实践
- Arthas实践--使用redefine排查应用奇怪的日志来源
- Arthas实践--快速排查Spring Boot应用404/401问题
- 阿里开源那个牛哄哄问题排查工具竟然不会用?最佳实践来了!
- 阿里开源那个牛哄哄问题排查工具竟然不会用?最佳实践来了!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Cult of the Amateur
Andrew Keen / Crown Business / 2007-6-5 / USD 22.95
Amateur hour has arrived, and the audience is running the show In a hard-hitting and provocative polemic, Silicon Valley insider and pundit Andrew Keen exposes the grave consequences of today’s......一起来看看 《The Cult of the Amateur》 这本书的介绍吧!