2

I am using the following code in a servlet of my app

java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage(str1);

When I run the application and call the servlet I get the following error

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

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

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
  • Which JDK do you use? From my experience most linux distributions don't provide the Sun/Oracle JDK by default and other JDKs might be missing that file (it should be located in `rt.jar` which should be on your classpath). – Thomas Apr 07 '11 at 06:43
  • I am using SUN JDK I am running on Linux Redhat I installed the SUN JDK 1.6.20 in it..and I have the `rt.jar` in my java installed at the location `/opt/jdk1.6.20/jre/lib` – Sangeet Menon Apr 07 '11 at 06:47

2 Answers2

5

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:

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It was working fine without any error or exception for quite a few days why would it start giving error suddenly?? – Sangeet Menon Apr 07 '11 at 07:30
  • S.M.09: The exact same code and no configuration change? No idea. It would have been useful if you'd said that to start with though. – Jon Skeet Apr 07 '11 at 07:40
  • 1
    @Jon Skeet: The exact same code and no configuration change? yes...no change at all...worked fine for quite few days...suddenly today in the morning I stared getting calls from everywhere!! Could it be some kind of corruption?? Cause changing the headless mode did make error disappear... – Sangeet Menon Apr 07 '11 at 07:50
  • @Jon Skeet: There is no internet connection its hosting apps for the local network only...(only for users within a building)..So an auto-update is out of the picture... – Sangeet Menon Apr 07 '11 at 07:55
  • @S.M.09: Okay, that's plain bizarre then. Any chance someone *had* been logged in via an X server which it happened to use, and now they've logged out, giving it nowhere to perform the operations? – Jon Skeet Apr 07 '11 at 07:57
  • @Jon Skeet: Not sure of that... perhaps I need to look at the logs and all more carefully...might be something would pop up...for the time being its working fine...is there a way to permanently change the mode of headless, instead of running every time in startup – Sangeet Menon Apr 07 '11 at 08:03
1

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.

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.

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.

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216