2015-10-06 129 views
1

我有一個非常簡單的REST API,它使用球衣2.x(Glassfish)。它在我什麼都不做時輸出,但輸出hello world string。 但是當我添加幾個罐子並在1行代碼中使用它們時,它會引發大量類別java.lang.NoClassDefFoundError錯誤。我沒有得到編譯或語法錯誤。這個錯誤很長,所以如果我提出了很多垃圾的話,請原諒。如何將jar文件添加到java rest api項目中?

我看到很多解決這個問題的方法,但我不明白他們的術語,例如你的類路徑需要修復等。

任何人都可以告訴我如何添加一個在代碼中導入的jar文件嗎?

代碼

package com.rest.example; 

import java.io.File; 

    import javax.ws.rs.GET; 
    import javax.ws.rs.Path; 
    import javax.ws.rs.PathParam; 
    import javax.ws.rs.core.Response; 

    import com.infineon.essence_210.api.Component; 
    import com.infineon.essence_210.api.EssenceDBMgr; 

    @Path("/testEssence") public class HelloWorld { 
     @GET 
     @Path("/{param}") 
     public Response getMsg(@PathParam("param") String msg){ 
      String output = "Welcome to the world of Rest : "+msg; 

      Component comp = null; 
      File file = new File("C:\\MYFILE.xml"); 
      try { 
          comp = EssenceDBMgr.getInstance().lookupComponent(file); //<---ISSUE HERE! 
       } catch (Exception e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
       } 
      return Response.status(200).entity(output).build(); 
     } 

    } 

錯誤在Eclipse控制檯(剪短,但詢問是否要全部)

SEVERE: Servlet.service() for servlet [exampleServlet] in context with path [/RestApi] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/sun/xml/bind/marshaller/NamespacePrefixMapper] with root cause java.lang.ClassNotFoundException: com.sun.xml.bind.marshaller.NamespacePrefixMapper at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1320) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1173) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2477) at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:880) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1290) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1173) at com.infineon.essence.jaxb.Unmarshaller.unmarshal(Unmarshaller.java:108) at com.infineon.essence.jaxb.Unmarshaller.unmarshal(Unmarshaller.java:88) at com.infineon.essence_210.api.Essence.unmarshal(Essence.java:201) at com.infineon.essence_210.api.Essence.unmarshal(Essence.java:223) at com.infineon.essence_210.api.Essence.unmarshal(Essence.java:229) at com.infineon.essence_210.api.Essence.(Essence.java:80) at com.infineon.essence_210.api.EssenceDBMgr.unmarshal(EssenceDBMgr.java:223) at com.infineon.essence_210.api.EssenceDBMgr.lookupComponent(EssenceDBMgr.java:1331) at com.rest.example.HelloWorld.getMsg(HelloWorld.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:317) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:298) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

+0

您是否嘗試過使用谷歌? http://oopbook.com/java-classpath-2/classpath-in-eclipse/ – Jens

+0

我確實。我用這裏提供的方法和'添加外部罐子'來添加罐子。在測試api工作時添加球衣罐時,我沒有任何問題,但後來我添加了另一個jar,在沒有錯誤的代碼中使用它,然後在運行後出現上述錯誤。 – Fearghal

回答

1

您可以將jar添加到您的lib項目的文件夾中。如果你想罐子添加到您的項目簡單,

同樣基於控制檯消息java.lang.NoClassDefFoundError: com/sun/xml/bind/marshaller/NamespacePrefixMapper,你缺少的jaxb-impl-jar文件。

而且您需要將lib文件夾下的所有罐子放在子文件夾中。

+0

我做了,他們成功添加。我用沒有問題的運動衫做了這件事。然後我添加了自己的罐子,然後在代碼中使用它們,在代碼時沒有問題,但隨後運行它,當我打開API網址時,出現上述錯誤。我真的很困惑,因爲錯過了什麼。 – Fearghal

+0

Thx for respionse,我加了這個,但沒有運氣。我能看到的是,這個特定的jar文件是在一個subb文件夾中的,我應該將所有的jar文件放在同一個文件夾中而沒有子文件夾? – Fearghal

+0

如果您有'lib'文件夾,則不需要將jar放在那裏。否則,您需要將jar添加爲外部jar,並參考上面答案中提到的jar的路徑。 – Lucky

相關問題