关于NoClassDefFoundError和ClassNotFoundException异常

https://www.iteye.com/blog/wxl24life-1919359

 

NoClassDefFoundError

ClassNotFoundException

Thrown if the Java Virtual Machine or a <code>ClassLoader</code> instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the <code>new</code> expression) and no definition of the class could be found.

Thrown when an application tries to load in a class through its string name but no definition for the class with the specified name could be found

可见前者是加载类定义而无定义抛出的问题(有类文件),后者是没有相关的类文件。碰到前面这个异常的解决办法,一般需要检查这个类定义中的初始化部分(如类属性定义、static 块等)的代码是否有抛异常的可能

 

 

如下图所示正是因为静态代码块中类ProcessInfoUtil中没有getCell()方法,导致类在初始化时就抛出异常,而后使用时就找不到Utils类定义。ProcessInfoUtil方法是在mns-invoker包中提供,需要升级相应包。

在升级mns-invoker包之后,再次启动tomcat但仍然抛出NoClassDefFoundError异常,且这次检查静态代码块,没有发现调用了不存在的方法

  • 静态代码块无明显异常时,抛出NoClassDefFoundError

由于静态代码块中没有明显异常,但抛出如下问题。此时需要进行debug

通过单步调试发现是在从package中获取所有的Class时抛出问题,如图所示。推测pigeon版本有问题,升级dpsf-net包后解决【注:具体问题还需要排查】

 

tips:项目中的jar包最好还是用inf-bom进行管理

 

 

今天遇到一个问题,需要通过知道的class文件得到该文件所在的jar包,试过很多办法都不行,最后在网上找到了一个解决办法,如下:

demo.java

1 2

String path = XXX.class.getProtectionDomain().getCodeSource().getLocation().getFile();

File jarFile = new File(path);

其中的XXX指已经知道的类名,然后通过后面的方法可以直接获取到JAR包路径,用于远程debug测试

 

 

posted @ 2020-04-12 22:23  岳兴亮  阅读(265)  评论(0编辑  收藏  举报