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


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/ 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( at java.awt.Window.( at java.awt.Frame.( at java.awt.Frame.( at javax.swing.JFrame.( at com.nated.framework.util.FileUtils.myOwnCropper(Unknown Source) at Source) at Source) at Source) at Source) at javax.servlet.http.HttpServlet.service( at javax.servlet.http.HttpServlet.service( at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( at org.apache.catalina.core.ApplicationFilterChain.doFilter( at org.apache.catalina.core.StandardWrapperValve.invoke( at org.apache.catalina.core.StandardContextValve.invoke( at org.apache.catalina.core.StandardHostValve.invoke( at org.apache.catalina.valves.ErrorReportValve.invoke( at org.apache.catalina.core.StandardEngineValve.invoke( at org.apache.catalina.connector.CoyoteAdapter.service( at org.apache.coyote.http11.Http11Processor.process( at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process( at$ at 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;