When is this exception thrown ?
java.awt.HeadlessException is thrown on a machine which is headless when -Djava.awt.headless commandline option is set to true and Heavyweight AWT components like Applet, Button, Checkbox, Choice, FileDialog, Label, List, Menu, MenuBar, MenuComponent, MenuItem, PopupMenu, Scrollbar,ScrollPane, TextArea, TextComponent, Frame, Window, Dialog, JApplet, JFrame, JWindow, JDialog and TextField are used in the environment.
What is the option -Djava.awt.headless all about?
Many environments, such as mainframe machines and dedicated servers, do not support a display, keyboard, or mouse. On Such machines if we try to to use AWT package,we will get an error like this
"Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
This error is thrown as the AWT package is causing the X11 server to open the DISPLAY.
To correct this problem, We need to tell the Java AWT package that the X11 server is headless (has no display).
To run our environment with a headless implementation, the follow property may be specified at the java command line:
This will tell the AWT package about the X11 server being headless.
This option is available from Java 1.4 onwards
Why we get a "HeadlessException" after setting this option ?
Headless support is enabled by the GraphicsEnvironment methods "isHeadless" and "isHeadlessInstance".
These methods check whether X11 server has a DISPLAY or its headless(without a DISPLAY).
The heavyweight AWT components like Applet, Button, Checkbox, Choice, FileDialog, Label, List, Menu, MenuBar, MenuComponent, MenuItem, PopupMenu, Scrollbar, ScrollPane, TextArea, TextComponent, Frame, Window, Dialog, JApplet, JFrame, JWindow, JDialog and TextField are affected by the lack of DISPLAY,keyboard and mouse etc.
Therefore ,if used in a "headless" environment they all will throw a "HeadlessException" like this -
How can we resolve this exception/related erros ?
-Check whether the X11 server is installed
-Check whether DISPLAY variable is properly set
-Heavyweight awt/swing components which requires display, mouse keyboard should be run on client side rather than server side.