线程“main”中的异常 java.lang.NoClassDefFoundError:无法初始化类 com.sun.jersey.core.header.MediaTypes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20487783/
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
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.core.header.MediaTypes
提问by anij
I'm trying to run a jersey client and facing this issue.
我正在尝试运行球衣客户端并面临此问题。
WS Class:
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();
}
}
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.accessjava.lang.NoClassDefFoundError: com/sun/jersey/spi/inject/Errors$Closure
java.lang.NoClassDefFoundError: com/sun/jersey/core/header/LinkHeaders
0(Client.java:118)
at com.sun.jersey.api.client.Client.f(Client.java:191)
at com.sun.jersey.api.client.Client.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, Hymanson-core-asl-1.9.2.jar, Hymanson-jaxrs-1.9.2.jar, Hymanson-mapper-asl-1.9.2.jar, Hymanson-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
asm-3.1.jar、Hymanson-core-asl-1.9.2.jar、Hymanson-jaxrs-1.9.2.jar、Hymanson-mapper-asl-1.9.2.jar、Hymanson-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
采纳答案by Satheesh Cheveri
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).
通常,当您的代码针对 jersey-bundle-1.8.jar 和 jsr311-api-0.9.jar 进行编译时,您会遇到此问题。但是在这里我可以看到您正在使用jsr311-api-1.1.1.jar
. 那么下一个问题将是旧的 jar 文件将被应用程序/网络服务器加载。例如:GlassFish 3.1 带有 Jersy 1.5(它可能优先于您的库)。
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
理想情况下,您需要检查服务器中加载的 JSR-311 库版本(jsr311-api jar 的 0.9 版本已过时)。您应该针对 jersey-bundle-1.8.jar 进行编译,并使用 jersey-bundle-1.8.jar 和 jsr311-api-1.1.1.jar 运行
回答by sudhir mohanraj
In my case I was not pulling in the jersey-core jar as a runtime dependency. Once i added it seemed to work fine.
就我而言,我没有将 jersey-core jar 作为运行时依赖项。一旦我添加它似乎工作正常。
回答by rogerdpack
For followers, these (with mockito in this instance, but kind of generic):
对于追随者,这些(在这种情况下使用 mockito,但有点通用):
##代码##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 :|
意味着“你依赖 jersey-core 1.0.2 并且依赖 jersey-client 1.11”(需要更紧密地匹配核心与客户端版本)。不幸的是,“服务器”和“客户端”都使用“核心”,因此最终它们几乎都必须精确匹配:|