java.lang.NoClassDefFoundError error when running my project

user352951 picture user352951 · May 7, 2014 · Viewed 11.7k times · Source

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?

Answer

Will picture Will · May 8, 2014

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