运行我的项目时出现 java.lang.NoClassDefFoundError 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23521647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:32:27  来源:igfitidea点击:

java.lang.NoClassDefFoundError error when running my project

javamavenjax-wsjax-rsjersey-client

提问by user352951

I have been struggling to get this to work and I think i can use some help. I am working on a Java project where the pom.xml has a bunch of dependencies some of which are themselves indirectly dependent on this jar: com.sun.jersey:jersey-core:1.17.1like this:

我一直在努力让它发挥作用,我想我可以使用一些帮助。我正在开发一个 Java 项目,其中 pom.xml 有一堆依赖项,其中一些本身间接依赖于这个 jar: com.sun.jersey:jersey-core:1.17.1像这样:

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.0.1</version>
</dependency>

And I need this particular jar in my pom because I want to use the new features in jax-rs api: javax.ws.rs:javax.ws.rs-api:2.0.The problem is when I build my project I get this error:

我需要在我的 pom 中使用这个特定的 jar,因为我想使用 jax-rs api 中的新功能: javax.ws.rs:javax.ws.rs-api:2.0。问题是当我构建我的项目时,我收到此错误:

Found duplicate classes in [com.sun.jersey:jersey-core:1.17.1,javax.ws.rs:javax.ws.rs-api:2.0] :
[WARNING]   javax.ws.rs.ApplicationPath
[WARNING]   javax.ws.rs.Consumes
[WARNING]   javax.ws.rs.CookieParam 
[WARNING]   javax.ws.rs.DELETE
[WARNING]   javax.ws.rs.DefaultValue
[WARNING]   javax.ws.rs.Encoded
[WARNING]   javax.ws.rs.FormParam
[WARNING]   javax.ws.rs.GET
[WARNING]   javax.ws.rs.HEAD
[WARNING]   javax.ws.rs.HeaderParam
...
..

I tried to fix this by excluding com.sun.jersey:jersey-core:1.17.1from dependencies which were including it by checking the dependency tree.

我试图com.sun.jersey:jersey-core:1.17.1通过检查依赖关系树从包含它的依赖项中排除来解决这个问题。

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.0.1</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
    </exclusion>
  </exclusions>
</dependency> 

Now the project builds fine but when I try to run it I get this error:

现在该项目构建良好,但是当我尝试运行它时,出现此错误:

    java.lang.NoClassDefFoundError: com/sun/jersey/core/util/FeaturesAndProperties
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.7.0_51]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531) ~[na:1.7.0_51]
at java.lang.Class.getDeclaredMethods(Class.java:1855) ~[na:1.7.0_51]
at com.google.inject.internal.ProviderMethodsModule.getProviderMethods
    (ProviderMethodsModule.java:81) ~[guice-3.0.jar:na]
at com.google.inject.internal.ProviderMethodsModule.configure
    (ProviderMethodsModule.java:73) ~[guice-3.0.jar:na]
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223) 
    ~[guice-3.0.jar:na]
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:232)
    ~[guice-3.0.jar:na]
at com.google.inject.spi.Elements.getElements(Elements.java:101) ~[guice-3.0.jar:na]
at com.google.inject.spi.Elements.getElements(Elements.java:92) ~[guice-3.0.jar:na]

It seems like jersey core was needed but how do I get around this problem?

似乎需要球衣核心,但我该如何解决这个问题?

采纳答案by Will

You can't mix JAX-RS/ Jerseyversions

你不能混合JAX-RS/Jersey版本

  • Jerseyversion 1is the reference implementation for JAX-RS 1. Jerseyversion 1uses the com.sun.jerseygroup / package prefix.

  • Jerseyversion 2is the reference implementation for JAX-RS 2. Jerseyversion 2uses the org.glassfish.jerseygroup / package prefix

  • Jersey版本1是 JAX-RS 1 的参考实现。Jersey版本1使用com.sun.jersey组/包前缀。

  • Jersey版本2是 JAX-RS 2 的参考实现。Jersey版本2使用org.glassfish.jersey组/包前缀

If you have both Jersey versions, or both JAX-RS versions on your classpath, you'll get lots of NoClassDefFoundError, NoSuchMethodErroror similar.

如果您的类路径上有两个 Jersey 版本,或两个 JAX-RS 版本,您将获得很多NoClassDefFoundErrorNoSuchMethodError或类似的。

If possible use JAX-RS/ Jerseyversion 2

如果可能,请使用JAX-RS/Jersey版本 2

回答by Pino

Jersey is the reference implementation of JAX-RS, so you don't need the latest version of JAX-RS API (javax.ws.rs:javax.ws.rs-api:2.0) but the latest version of Jersey which is 2.8 (see https://jersey.java.net/).

Jersey 是 JAX-RS 的参考实现,因此您不需要最新版本的 JAX-RS API ( javax.ws.rs:javax.ws.rs-api:2.0) 而是最新版本的 Jersey 2.8(请参阅https://jersey.java.net/)。

回答by rogerdpack

For me this actually means I had somehow mixed things and was using

对我来说,这实际上意味着我以某种方式混合了东西并且正在使用

$ mvn dependency:list | grep jersey
[INFO]    com.sun.jersey:jersey-client:jar:1.11:compile
[INFO]    com.sun.jersey:jersey-core:jar:1.0.2:compile

(conflict within jersey 1.x)

(球衣 1.x 内的冲突)