java.lang.NoClassDefFoundError error when running my project
Asked Answered
L

3

7

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.1 like this:

<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:

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.1 from dependencies which were including it by checking the dependency tree.

<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?

Lurlene answered 7/5, 2014 at 15:12 Comment(0)
E
9

You can't mix JAX-RS / Jersey versions

  • Jersey version 1 is the reference implementation for JAX-RS 1. Jersey version 1 uses the com.sun.jersey group / package prefix.

  • Jersey version 2 is the reference implementation for JAX-RS 2. Jersey version 2 uses the org.glassfish.jersey group / package prefix

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

If possible use JAX-RS / Jersey version 2

Eunuchoidism answered 8/5, 2014 at 9:19 Comment(0)
I
1

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)

Indene answered 12/10, 2017 at 16:12 Comment(0)
K
0

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/).

Klutz answered 7/5, 2014 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.