简体   繁体   English

Servlet给出错误java.lang.NoClassDefFoundError

[英]Servlet giving error java.lang.NoClassDefFoundError

I am using the following code in a servlet of my app 我在我的应用程序的servlet中使用以下代码

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$2.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 ...

To use AWT classes in a server side application, I believe you need to run in "headless" mode. 要在服务器端应用程序中使用AWT类,我相信您需要在“无头”模式下运行。 Change your servlet container's startup to include: 将您的servlet容器的启动更改为包括:

-Djava.awt.headless=true

(Or set the system property within your own code if you really have to.) (或者如果确实需要,可以在自己的代码中设置系统属性。)

You might also want to consider using an alternative imaging library - either a third-party one or the javax.imageio package. 您可能还需要考虑使用替代的映像库-第三方库或javax.imageio包。

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"; 但是,我可能会猜测一个根本原因是运行Web管理员的JVM是“无头的”。 ie it doesn't have an accessible display. 即它没有可访问的显示。

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. 该解决方案可能很简单, -Djava.awt.headless=true在Web容器启动脚本的JVM选项中添加-Djava.awt.headless=true即可。 For instance, if you are using Tomcat, add that to the $JAVA_OPTS environment variable before calling catalina.sh . 例如,如果使用的是Tomcat,则在调用catalina.sh之前将其添加到$JAVA_OPTS环境变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM