线程“main”中的异常java.lang.NoClassDefFoundError:无法初始化类com.sun.jersey.core.header.MediaTypes [英] Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.core.header.MediaTypes

查看:198
本文介绍了线程“main”中的异常java.lang.NoClassDefFoundError:无法初始化类com.sun.jersey.core.header.MediaTypes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个球衣客户端并面临这个问题。

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

WS等级:

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();

}

}

客户类别:

    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)

我正在使用以下罐子:

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

推荐答案

一般来说你会得到这个专业版当您的代码针对jersey-bundle-1.8.jar和jsr311-api-0.9.jar编译时出现错误。但在这里我可以看到你正在使用 jsr311-api-1.1.1.jar 。然后下一个问题是应用程序/ Web服务器将加载旧的jar文件。例如:GlassFish 3.1一个附带Jersy 1.5(可能优先于你的库)。

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

理想情况下你需要检查加载的JSR-311库的版本(服务器中的0.9版本的jsr311-api jar已经过时了。
您应该针对jersey-bundle-1.8.jar进行编译,并使用jersey-bundle-1.8.jar和jsr311-api-1.1.1.jar运行

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

这篇关于线程“main”中的异常java.lang.NoClassDefFoundError:无法初始化类com.sun.jersey.core.header.MediaTypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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