在Tomcat下访问servlet时出现java.lang.NoClassDefFoundError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/857409/
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
Under Tomcat java.lang.NoClassDefFoundError when accessing a servlet?
提问by Omar Kooheji
I'm writing a web applicaion in Eclipse, and I'm using the XStream Serialization Library to generate My JSON.
我正在 Eclipse 中编写一个 Web 应用程序,并且我正在使用 XStream 序列化库来生成我的 JSON。
I've encapsulated the Code that builds up the JSON in a class which is called by my servelet. Then encapsulated class has a main method for testing and when I run it everything seems to work fine.
我已经将构建 JSON 的代码封装在一个由我的小服务器调用的类中。然后封装的类有一个主要的测试方法,当我运行它时,一切似乎都正常。
However when I use the call the class from my servelet I get a java.lang.NoClassDefFoundError
error, sayying that I've not loaded the XStream libraries. I assume I've got my build path wrong, but I've set the XStream libraries to be in the build path for the project, so as far as I know it should work.
但是,当我使用从我的小服务器调用类时,我收到一个java.lang.NoClassDefFoundError
错误,说我没有加载 XStream 库。我假设我的构建路径有误,但我已将 XStream 库设置为项目的构建路径,据我所知它应该可以工作。
What is likely to be going wrong here?
这里可能会出现什么问题?
Following is the exact exception:
以下是确切的例外:
java.lang.ClassNotFoundException: com.thoughtworks.xstream.io.HierarchicalStreamDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at SecurePaymentAjaxData.doPost(SecurePaymentAjaxData.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
And here is the relavant code that uses the xstream class:
这是使用 xstream 类的相关代码:
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("CallDataUpdate", CallDataUpdate.class);
xstream.alias("CardStatus", CardStatus.class);
String jsonData = xstream.toXML(updateData);
I stress that this code works fine when run as a java application, I'm sure it's something to do with loading the libraries, I just don't know where I've gone wrong.
我强调这段代码在作为 Java 应用程序运行时可以正常工作,我确定这与加载库有关,我只是不知道我哪里出错了。
采纳答案by Omar Kooheji
I found the issue:
我发现了这个问题:
If you right click on the project and select properties you can set the "Java EE Module dependencies" to include the modules you are using.
如果您右键单击项目并选择属性,您可以设置“Java EE 模块依赖项”以包含您正在使用的模块。
Hey Presto it works.
嘿 Presto 它有效。
回答by Brian Agnew
Don't forget that there's a difference between how you buildand how you deploy. That is, you may be building against XStream, but you should also package it in the .war file for deployment.
不要忘记构建和部署的方式是不同的。也就是说,您可能正在针对 XStream 进行构建,但您也应该将其打包在 .war 文件中以进行部署。
Explode your resultant .war file (jar tvf {filename}
to see the contents) and check whether it's there (a .jar file under WEB-INF/lib
). I suspect it's not.
分解生成的 .war 文件(jar tvf {filename}
查看内容)并检查它是否存在(在 .jar 文件下WEB-INF/lib
)。我怀疑不是。
回答by Omar Kooheji
A easy mistake to make is to misname the directory under WEB_INF, I've named it libs instead of lib, compiles fine but tomcat throws errors.
一个容易犯的错误是错误命名了 WEB_INF 下的目录,我将它命名为 libs 而不是 lib,编译正常但 tomcat 会抛出错误。
回答by Florent
Java EE Module Dependencies seem to be the way to go. However, menus names changed in Eclipse Helios: that particular menu is now under "Deployment Assembly".
Java EE Module Dependencies 似乎是要走的路。但是,Eclipse Helios 中的菜单名称发生了变化:该特定菜单现在位于“部署程序集”下。