e世博体育

首页 > 正文

通过IDEA快速定位和排除依赖冲突

www.buy-cialis-enus.com2019-07-15
e世博体育平台

Our programmers often encounter various bugs during development, most of which are business logic exceptions, and some are exceptions caused by improper code writing.

For example: NullPointException (NPE), IndexOutOfBoundsException, etc. In fact, we are all well positioned and fixed. However, there are some runtime exceptions that are particularly troublesome to locate, which is an exception caused by jar package conflicts.

The general program runs at runtime similar to java.lang.ClassNotFoundException, Method not found: '.', or inexplicable exception information, which is generally caused by the problem of jar package dependency conflicts.

As for why the jar package dependency conflict occurs? This kind of problem can be roughly summarized as follows:

Versions do not match, high versions rely on lower versions, or lower versions rely on higher versions. For example, a third-party library is introduced, but the third-party library is based on JDK7, and your project uses JDK8. Repeated introduction of different versions of the jar package, resulting in usage errors. Many times we introduce third-party wheels, they rely on the introduction of a basic tool using the v 1.0 jar, but we also introduced the jar in our project, but the version is v 2.3, which will cause the same project in the project. The component but relies on two different versions of the jar, conflicts will occur.

As you can see, in general, the main reason for jar package conflicts is the dependency version conflict.

In the project, the report needs to be exported. When the technology is selected, the Apache POI is generally used. However, the use of the POI is relatively basic, and the development volume is large, which is prone to memory overflow.

xxConsidering that Ali has open sourced a set of tools for parsing and generating Excel - EasyExcel, which has the problem of avoiding memory overflow OOM, and is easy to use, so it was introduced into our project, the specific version is 1.0.2.

Com.alibaba

Easyexcel

1.0.2

The other module needs to use POI to convert Word to PDF, so it also introduces the following POI dependencies:

Org.apache.poi

Poi

3.15

Org.apache.poi

Poi-ooxml

3.15

We can see from the Maven Repository that Ali EasyExcel 1.0.2 relies on a POI of 3.15, so it should be ok.

7444f908a2094bbcba6cce22dfa944cf

However, there is still a problem when debugging the interface, and the exception information is very strange. It is not a glance to know the cause of the problem and solve it.

Caused by: java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z

xxAt com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java: 377)

At com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java: 131)

At com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java: 98)

At com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java: 693)

At com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java: 737)

At com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java: 351)

xx在org.apache.poi.openxml4j.opc.StreamHelper.saveXmlInStream(StreamHelper.java: 80)

在org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller.marshallRelationshipPart(ZipPartMarshaller.java: 181)

在org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java: 560)

在org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java: 1557)

在org.apache.poi.POIXMLDocument.write(POIXMLDocument.java: 248)

在org.apache.poi.xssf.streaming.SXSSFWorkbook.write(SXSSFWorkbook.java: 941)

在com.alibaba.excel.write.ExcelBuilderImpl.finish(ExcelBuilderImpl.java: 64)

在com.alibaba.excel.ExcelWriter.finish(ExcelWriter.java: 95)

在com.pingan.haofang.creams.common.utils.ExcelUtil.writeExcel(ExcelUtil.java: 71)

.

.省略了65个常见帧

提取密钥信息,可以看到错误类型java.lang.AbstractMethodError,这种错误类型称为抽象方法错误。这种类型的错误类似于我们上面所说的ClassNotFoundException,并且很可能是由Jar包依赖性冲突引起的。

然后让我们找出哪些jar包冲突。只需排除冲突的jar包并保留正确的jar包。

我们可以看到错误类型是java.lang.AbstractMethodError,错误类型后跟一个特定的错误消息描述:org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z,意思是包org.apache。 xerces.dom类DocumentImpl在其方法getXmlStandalone()调用中有错误。

那么谁具体打电话?我们可以在下一行异常信息中看到以下代码行:

在com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java: 377)

在包路径com.sun.org.apache.xalan.internal.xsltc.trax中,DOM2TO类代码的第377行具有setDocumentInfo方法。我们单击鼠标左键并在该行中添加Debug断点。

ef5d83437f4d4e388aeff395bc011b81

我们发现这个DOM2TO类位于JDK1.8的rt.jar包中。具体的类路径如下:

544a9c0a68d14383a84ad45a4c326ae6

通过断点调试,文档对象是DocumentImpl实例,

176775391065462ab88c75c8ddb45bbd

此DocumentImpl的实际路径也位于JDK1.8的rt.jar包中,该包是CoreDocumentImpl的子类,它是接口Document的实现类。

包com.sun.org.apache.xerces.internal.dom;

公共类DocumentImpl

扩展CoreDocumentImpl

实现DocumentTraversal,DocumentEvent,DocumentRange {

.

}

CoreDocumentImpl

包com.sun.org.apache.xerces.internal.dom;

公共类CoreDocumentImpl

扩展ParentNode实现Document {

.

}

我们在CoreDocumentImpl类的第983行找到了getXmlStandalone方法。

对我们来说很明显,DOM2TO类中setDocumentInfo方法的参数Document是JDK1.8中rt.jar包中classpath com.sun.org.apache.xerces.internal.dom下的实现类DocumentImpl。我们提供错误的信息是:

引起: java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z

这个org.apache.xerces.dom.DocumentImpl显然不属于我们的JDK1.8 rt.jar包,并且没有getXmlStandalone方法。

2670945e255243e2bfa2a0e909a1fd8d

所以得知,我的项目中罐包依赖冲突了,我们只需要排除掉org.apache.xerces.dom.DocumentImpl所属的jar包就可以了。如何排除呢?

我们在IDEA中双击Shift键,输入DocumentImpl,得到如下结果:

347b2a6a24a34b8791338e49c5238eab

可以发现,这里有两个CoreDocumentImpl,一个是我们的JDK1.8的,一个是属于xerce的,而且确实在依赖的maven jar包中发现了xercesImpl-2.4.0.jar,这个jar包就是需要排除的罐子包。

69377fe051d24ec19ffb722045003fea

发现了冲突的jar包,我全局搜索关键字xerces,并没有发现哪一个pom中有依赖的代码,所以很可能是其他的jar包传递依赖进来的。

我们借助IDEA的maven工具,在maven栏右键项目模块,选择show Dependencies或Ctrl + Shift + Alt + U,这时候会展示当前模块的jar包依赖图,如下:

3264dbc744ea455488b1852f9179f7dc

虽然这里展示了很多冲突的jar包,其中红线连接的就是冲突的jar包,但是我们Ctrl + F查询xerces还是没有结果。

XX所以我们需要一个额外的解决方法,然后我认为IDEA有一个插件Maven Helper,具体的插件下载可以参考之前的内容,下载插件后,我们打开pom.xml文件,在pom的左下方.xml文件有一个Dependency Analyzer,我们在点击:

后显示

d6c65e2036dc450ebae9e684bbc281a6

冲突:显示所有冲突。所有依赖关系列表:显示列表中的所有依赖关系。作为树的所有依赖关系:显示树中的所有依赖关系。

我们输入xerces,选择在树中显示所有依赖项,并获取以下信息。

98610de7ec6c4bd8908e9df61ba4c406

显然,最初的罪魁祸首是由file-web-sdk引入的。右键单击Jump To Source或F4以在pom.xml依赖项中找到jar。如下所示,我们从排除标记中排除xercesImpl。介绍可以。

Com.xx.xx.gov.fileservice

文件的Web-SDK

的Xerces

xercesImpl

再次启动项目,测试界面发现功能正常,整个故障排除过程结束,IDEA功能仍然非常强大。

很多时候jar包都会发生冲突,其中一些很容易被排除。例如,在pom.xml中我们可以找到一些重复的介绍,但版本不一样。还有其他依赖项传入。我们无法在pom.xml文件中直观地找到它。此时,我们可以借助工具找到冲突的依赖关系。

但是还有更多隐藏的冲突,比如本文中描述的依赖冲突。此时我们需要分析异常信息,找出冲突的原因并找到特定冲突的依赖关系,最后将其排除。

本文提供了对异常分析和冲突定位以及最终排除的更详细介绍。可以以上述方式检查类似的依赖性冲突。我希望本文能帮助您解决项目中的冲突。

Cnblogs.com/jajian/p/11002521.html

回复“资源”,接收动手源代码,视频教程,微服务,并发,数据可调等,搜索[Java Companion]

0454d50e104b436f8c0f87ec193b35a0

热门浏览
热门排行榜
热门标签
日期归档