java.awt.HeadlessException – Applet not displayed – Part 2 – Education Career Blog

This is with reference to question java.awt.HeadlessException – Applet not displayed.
java.awt.HeadlessException – Applet not displayed

The HeadlessException went away after I added “export DISPLAY=:0.0” in tomcat’s startup.sh file. Now some part of the code is run on a batch server which is a separate server. The same HeadlessException occurs when the code is invoked on the batch server. When I added the same “export DISPLAY=:0.0” in batch server’s startup file, the exception went away but it created problem for other (non java) applications running on the batch server. This is probably because we are overriding the display that is working for other applications.

Next I exported JAVA_OPTS=”-Djava.awt.headless=true” to batch server’s startup file, but it did not work. I saw that the following link from Sun says that code should check for headless exception.

http://java.sun.com/j2se/1.4.2/docs/guide/awt/AWTChanges.html

So will catching the headless exception make it work? Like:

try {

 //Code that throws headless exception

} catch (HeadlessException e) {

  printStacktrace(); //basically do nothing
}

,

You could catch that exception or you could avoid it by checking first:

if (! java.awt.GraphicsEnvironment.isHeadless()) {
    // code that throws headless exception
} else {
  log.info("Skipping GUI portion")
}

Don’t set a DISPLAY on a server that no one is going to look at, or you might end up waiting forever for someone to click OK on a dialog that no one can see.

Leave a Comment