Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.core.header.MediaTypes
Asked Answered
H

3

10

I'm trying to run a jersey client and facing this issue.

WS Class:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

@GET
@Path("/vip")
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@QueryParam("msg") String msg) {

    String output = "Jersey say : " + msg;

    return Response.status(200).entity(output).build();

}

}

Client Class:

    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.ClientResponse;
    import com.sun.jersey.api.client.WebResource;

    public class JerseyClientGet {

        public static void main(String[] args) {
            try {

                Client client = Client.create();

                WebResource webResource = client
                        .resource("http://localhost:8080/TestRest/rest/hello/vip?msg=ABCD");

                ClientResponse response = webResource.accept("application/json")
                        .get(ClientResponse.class);

                if (response.getStatus() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + response.getStatus());
                }

                String output = response.getEntity(String.class);

                System.out.println("Output from Server .... \n");
                System.out.println(output);

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

Issue is when I'm trying to execute the Client Class, following error message is coming

            Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.core.header.MediaTypes
                at com.sun.jersey.core.spi.factory.MessageBodyFactory.initReaders(MessageBodyFactory.java:182)
                at com.sun.jersey.core.spi.factory.MessageBodyFactory.initReaders(MessageBodyFactory.java:176)
                at com.sun.jersey.core.spi.factory.MessageBodyFactory.init(MessageBodyFactory.java:162)
                at com.sun.jersey.api.client.Client.init(Client.java:342)
                at com.sun.jersey.api.client.Client.access$000(Client.java:118)
                at com.sun.jersey.api.client.Client$1.f(Client.java:191)
                at com.sun.jersey.api.client.Client$1.f(Client.java:187)
                at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
                at com.sun.jersey.api.client.Client.<init>(Client.java:187)
                at com.sun.jersey.api.client.Client.<init>(Client.java:159)
                at com.sun.jersey.api.client.Client.create(Client.java:669)
                at com.mkyong.client.JerseyClientGet.main(JerseyClientGet.java:12)

I'm using the following jars:

asm-3.1.jar, jackson-core-asl-1.9.2.jar, jackson-jaxrs-1.9.2.jar, jackson-mapper-asl-1.9.2.jar, jackson-xc-1.9.2.jar, jersey-bundle-1.8.jar, jersey-client-1.18.jar, jersey-core-1.18.jar, jersey-json-1.18.jar, jersey-server-1.18.jar, jersey-servlet-1.18.jar, jettison-1.1.jar, jsr311-api-1.1.1.jar

Hypochondriac answered 10/12, 2013 at 6:36 Comment(4)
jersey-core is needed I guess. Check if you have MediaTypes class in this jar.Inexorable
Do you have the same error in Server console also?Jerejereld
Thanks for the quick reply, well I'm using jersey-core-1.18.jar which contains MediaTypes class. But still its showing this ExceptionHypochondriac
@SatheeshCheveri well my WS class is deployed in tomcat, where there is no exception, but the Client class i'm executing as main method.Hypochondriac
J
10

Generally you would get this problem when your code compiled against jersey-bundle-1.8.jar and jsr311-api-0.9.jar. But here I can see you are using jsr311-api-1.1.1.jar. Then next problem would be older jar file would have been loaded by the application/web server. For eg: GlassFish 3.1 one comes with Jersy 1.5( which may take precedence over your libraries).

Ideally you would need to check version of JSR-311 library is loaded (0.9 version of the jsr311-api jar is obsolete) in the server. And you should compile against jersey-bundle-1.8.jar, and run with jersey-bundle-1.8.jar and jsr311-api-1.1.1.jar

Jerejereld answered 10/12, 2013 at 6:47 Comment(3)
Thanks a lot Satheesh! After adding these jars in my server lib folder, its working fine.Hypochondriac
Yes its working fine now, I just add these jars in my apache-tomcat-7.0.42 server's lib folder, and restarted the server, now, its working fine. Thanks a lot man!Hypochondriac
well how to do that, i tried to vote but as my reputation is below 15, i cant.Hypochondriac
P
2

In my case I was not pulling in the jersey-core jar as a runtime dependency. Once i added it seemed to work fine.

Procuration answered 13/5, 2015 at 14:53 Comment(1)
theoretically maven should be pulling in "a version" of jersey-core for you already (check with $mvn dependency:tree) but if it's not the right version then yeah you'll need to call it out and explicitly state the version you want.Arst
A
0

For followers, these (with mockito in this instance, but kind of generic):

java.lang.NoClassDefFoundError: com/sun/jersey/spi/inject/Errors$Closure

java.lang.NoClassDefFoundError: com/sun/jersey/core/header/LinkHeaders

meant "you have a dependency on jersey-core 1.0.2 and also dependency on jersey-client 1.11" (need to match core with client versions more closely). Unfortunately "server" and "client" both use the "core" so in the end they practically all have to precisely match up :|

Arst answered 8/11, 2016 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.