java.awt.HeadlessException after setting java.awt.headless=true

Discussions

General J2EE: java.awt.HeadlessException after setting java.awt.headless=true

  1. Hi, I am using tomcat running on Linux box. In my application I have used classes from java.awt.* package. To get the all these running properly in the server startup script (/usr/local/tomcat/bin/startup.sh) I have added the following lines: JAVA_OPTIONS="${JAVA_OPTIONS} ${JAVA_PROPERTIES} -Djava.awt.headless=true" export JAVA_OPTIONS And in my java application code I have written the following line of codes to test the Headless mode: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); System.out.println("Headless mode: " + ge.isHeadless()); if(!ge.isHeadless()){ System.setProperty("java.awt.headless", "true"); } The out put of the System.out.println statement is printing out "Headless mode: true". Still I am getting the following exception: INFO: Execute Method Ends without Error java.awt.HeadlessException at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159) at java.awt.Window.(Window.java:406) at java.awt.Frame.(Frame.java:402) at java.awt.Frame.(Frame.java:367) at javax.swing.JFrame.(JFrame.java:163) at com.nated.framework.util.FileUtils.myOwnCropper(Unknown Source) at com.nated.app.common.action.CropProfileImageAction.execute(Unknown Source) at com.nated.app.user.servlet.HomePageServlet.moduleProcess(Unknown Source) at com.nated.app.user.servlet.HomePageServlet.process(Unknown Source) at com.nated.app.user.servlet.HomePageServlet.doPost(Unknown Source) at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 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:175) 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:263) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619) I am not able to understand what else needs to be done to resolve this problem. I have done googling regarding this issue but getting nothing different. Can any body suggest anything to resolve this issue? I am stuck with this problem for 2 days and not able to proceed further. Thanks in advance for giving me your valuable time. Thanks, Rajkumar
  2. Try checking that the argument is getting picked up correctly, check the value of System.getProperty("java.awt.headless"). Also, there's no need to instantiate a JFrame in a server side application. If you are using a 3rd party library in your server side code that uses Swing under the covers you have no choice but to set headless mode to true, but if not I would recommend removing the Swing dependencies from your code.
  3. Hi Jason, Thanks for your time and help. I would have been the happiest person if I could remove the Swing dependencies from my server side codes. Today I will not be able to make builds in Linux environment because of the Holidays but I will surely check the System.getProperty("java.awt.headless") on the next working day and will get back to you ASAP. For your information I have used the following packages in my server side class: import java.awt.Container; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.MediaTracker; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; These classes are required for cropping an image. I would also like to mention that in my code I have checked the isHeadless() property of the GraphicsEnvironment.getLocalGraphicsEnvironment() and it is getting printed as true. Do you think it can be different from System.getProperty("java.awt.headless")? Thanks, Raj Kumar Kundu
  4. Hi Jason, I have checked the System.getProperty("java.awt.headless") from my java program and it is getting displayed as "true". Do you have any suggestion for me? I am still stuck with this problem. I have tried the same in UNIX environment and able to see same problem. Am I doing something wrong? Thanks again in advance for your time and help. Regards, Rajkumar
  5. Hi Rajkumar, I am facing precisely the same issue. Did you get any solution for this? Thanks, Janaki
  6. Hi Rajkumar, I could resolve my issue by using Panel instead of Frame in my code. It seems Panel and Canvas are the only two AWT components supported in headless mode. Since my code was not relying on heavyweight AWT components, I could change to Panel very easily. I hope this helps in case you are still stuck up. Thanks, Janaki
  7. I have resolved this problem by not using JFrame. Instead of JFrame I have used the following: FilteredImageSource myFilteredImageSource = new FilteredImageSource(image.getSource(), new CropImageFilter(x, y, crpHeight, crpWidth)); image = Toolkit.getDefaultToolkit().createImage(myFilteredImageSource); try { MediaTracker mediaTracker = new MediaTracker(new Container()); ... I have used the following awt imports in my code: import java.awt.Image; import java.awt.MediaTracker; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource;