Servlet 给出错误 java.lang.NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5576986/
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
Servlet giving error java.lang.NoClassDefFoundError
提问by Sangeet Menon
I am using the following code in a servlet of my app
我在我的应用程序的 servlet 中使用以下代码
java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage(str1);
java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage(str1);
When I run the application and call the servlet I get the following error
当我运行应用程序并调用 servlet 时,出现以下错误
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11.XToolkit
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
java.awt.Toolkit.run(Toolkit.java:834)
java.security.AccessController.doPrivileged(Native Method)
java.awt.Toolkit.getDefaultToolkit(Toolkit.java:826)
noticeandreports.pdf.appendFiles.PdfFunctionsClass.addSealSpace(PdfFunctionsClass.java:198)
OJ.NoticesandReports.generate_151_OJNotice.execute(generate_151_OJNotice.java:768)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have hosted the app on a Linux machine with Java version JDK 1.6.20..
我已经在使用 Java 版本 JDK 1.6.20 的 Linux 机器上托管了该应用程序。
What might be causing the issue...
可能是什么导致了这个问题...
noticeandreports.pdf.appendFiles.PdfFunctionsClass
is the class where the code is written and OJ.NoticesandReports.generate_151_OJNotice
is the servlet that calls the method inside the above class...
noticeandreports.pdf.appendFiles.PdfFunctionsClass
是编写代码的类,是OJ.NoticesandReports.generate_151_OJNotice
调用上述类中方法的servlet...
回答by Jon Skeet
To use AWT classes in a server side application, I believe you need to run in "headless"mode. Change your servlet container's startup to include:
要在服务器端应用程序中使用 AWT 类,我相信您需要在“无头”模式下运行。更改 servlet 容器的启动以包括:
-Djava.awt.headless=true
(Or set the system property within your own code if you reallyhave to.)
(或者如果你真的需要,在你自己的代码中设置系统属性。)
You might also want to consider using an alternative imaging library - either a third-party one or the javax.imageio
package.
您可能还想考虑使用替代的图像库 - 第三方库或javax.imageio
包。
回答by Stephen C
That is almost certainly not the complete stack trace. Either that stack trace or an earlier one in the log file will tell you what caused the initialization of sun.awt.X11.XToolkit
to fail.
这几乎肯定不是完整的堆栈跟踪。日志文件中的堆栈跟踪或较早的跟踪将告诉您导致初始化sun.awt.X11.XToolkit
失败的原因。
However, I'd hazard a guess that the root cause is that the JVM running the web countainer is "headless"; i.e. it doesn't have an accessible display.
但是,我猜测根本原因是运行 Web 计数器的 JVM 是“无头的”;即它没有可访问的显示。
The Oracle Java Technical Article entitled "Using Headless Mode in the Java SE Platform"(by Artem Ananiev and Alla Redko, June 2006) describes the issue and what to do about it.
题为“在 Java SE 平台中使用无头模式”的 Oracle Java 技术文章(Artem Ananiev 和 Alla Redko,2006 年 6 月)描述了该问题以及如何处理。
The solution is probably as simple as adding -Djava.awt.headless=true
to the JVM options in the web container startup script. For instance, if you are using Tomcat, add that to the $JAVA_OPTS
environment variable before calling catalina.sh
.
解决方案可能就像-Djava.awt.headless=true
在 Web 容器启动脚本中添加到 JVM 选项一样简单。例如,如果您使用 Tomcat,请$JAVA_OPTS
在调用catalina.sh
.