ClassNotFoundException:如何在Java中查找依赖项冲突 [英] ClassNotFoundException: how to find dependency conflict in Java

查看:389
本文介绍了ClassNotFoundException:如何在Java中查找依赖项冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Atmosphere servlet的测试WebSocket应用程序中,出现以下异常:

In a test WebSocket application using Atmosphere servlet I'm getting the following exception:

SEVERE: Servlet.service() for servlet AtmosphereServlet threw exception
java.lang.ClassNotFoundException: javax.servlet.AsyncContext
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
    at org.atmosphere.cpr.AtmosphereServlet.doPost(AtmosphereServlet.java:191)
    at org.atmosphere.cpr.AtmosphereServlet.doGet(AtmosphereServlet.java:177)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

从下面的帖子中,我了解到这可能是由Servlet 3.0之前的Servlet容器版本引起的.

From the below posts I understand that this might be caused by a Servlet container version older than Servlet 3.0:

ClassNotFoundException:Jetty问候世界中的javax.servlet.AsyncContext

ClassNotFoundException:Jetty中的javax.servlet.AsyncContext eclipse中的hello世界

Grails项目-Servlet调用-ClassNotFoundException:javax.servlet.AsyncContext

但是该应用程序正在Tomcat7上运行,并且在pom.xml中添加了以下依赖项:

However the application is running on Tomcat7, and the following dependency is added in pom.xml:

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

我已经检查了项目中的所有其他依赖关系,但找不到与Servlet相关的任何其他内容.仍然有例外.

I've checked all other dependensies in the project and was not able to find anything else related to Servlet. Still I'm getting the exception.

问题:如何查找该应用程序实际使用的jar文件?如何找到导致使用旧版本的依赖项?

Questions: How to find a jar file which is actually used by the application? How to find the dependency which is causing the usage of an old version?

推荐答案

我终于可以通过执行以下操作来解决依赖项冲突.

I was finally able to solve the dependency conflict by doing the following.

要找到应用程序使用的jar文件,我使用了以下简单代码:

To find the jar file which is used by application I used the below simple code:

public void listJarFilesAndClassVersions() {    
    Class classToCheck = javax.servlet.ServletRequestWrapper.class;
    URL location = classToCheck.getResource('/'
        + classToCheck.getName().replace('.', '/') + ".class");

    System.out.println(location.toString());

    for(Package p : Package.getPackages()) {
        if (p.getName().startsWith("javax.servlet")) {
            System.out.println("Class: " + p.getName()
                + ", version: " + p.getSpecificationVersion());
        }
    }
}

选择了 javax.servlet.ServletRequestWrapper 类,因为它确实存在于旧的 Servlet 2.5 中.

The class javax.servlet.ServletRequestWrapper was chosen because it does exist in the old Servlet 2.5.

上面脚本的执行为我提供了以下内容:

The execution of the above script gives me the following:

jar:file:/C:/Users/[username]/.m2/repository/org/apache/tomcat/servlet-api/6.0.29/servlet-api-6.0.29.jar!/javax/servlet/ServletRequestWrapper.class
Class: javax.servlet.jsp, version: 2.1
Class: javax.servlet, version: 2.5
Class: javax.servlet.http, version: null

因此,首先,它确认使用版本2.5的Servlet,其次,坏" jar位于tomcat目录下的maven存储库中.

So, first, it confirms that the Servlet of version 2.5 is used, and second, the "bad" jar is located in the maven repository under the tomcat directory.

经过短暂的研究,我终于找到了根本原因:在部署和运行Maven应用程序时,我需要指定tomcat的具体版本,否则maven使用版本6的tomcat中的库.对我来说就是改变

After a short reasearch I was finally able to find the root cause of that: when deploying and running the maven application I need to specify the concrete version of tomcat, otherwise maven uses the libraries from tomcat of version 6. So the fix for me was to change

mvn -Dmaven.tomcat.port=8080 tomcat:run-war

mvn -Dmaven.tomcat.port=8080 tomcat7:run-war

现在,如果我执行上述脚本,它将得到以下结果:

Now if I execute the above script it gives the following result:

jar:file:/C:/Users/[username]/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/7.0.47/tomcat-embed-core-7.0.47.jar!/javax/servlet/ServletRequestWrapper.class
Class: javax.servlet.jsp, version: 2.2
Class: javax.servlet, version: 7.0
Class: javax.servlet.http, version: 7.0
Class: javax.servlet.annotation, version: 7.0
Class: javax.servlet.descriptor, version: 7.0

希望它可以帮助遇到相同问题的其他人.

Hope it helps others who is running into the same issue.

这篇关于ClassNotFoundException:如何在Java中查找依赖项冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆