Using Headless Mode in the Java SE Platform
By Artem Ananiev and Alla Redko, June 2006
This article explains how to use the headless mode capabilities of the Java Platform, Standard Edition (Java SE, formerly referred to as J2SE).
Headless mode is a system configuration in which the display device, keyboard, or mouse is lacking. Sounds unexpected, but actually you can perform different operations in this mode, even with graphic data.
Where it is applicable? Let’s say that your application repeatedly generates a certain image, for example, a graphical authorization code that must be changed every time a user logs in to the system. When creating an image, your application needs neither the display nor the keyboard. Let’s assume now that you have a mainframe or dedicated server on your project that has no display device, keyboard, or mouse. The ideal decision is to use this environment’s substantial computing power for the visual as well as the nonvisual features. An image that was generated in the headless mode system then can be passed to the headful system for further rendering.
Many methods in the java.awt.Toolkit and java.awt.GraphicsEnvironment classes, with the exception of fonts, imaging, and printing, require the availability of a display device, keyboard, and mouse. But some classes, such as Canvas or Panel, can be executed in headless mode. Headless mode support has been available since the J2SE 1.4 platform.
Note: This article will point the reader to documentation for version 6 of the Java SE platform. Any API additions or other enhancements to the Java SE platform specification are subject to review and approval by the JSR 270 Expert Group.
![]()
The java.awt.Toolkit class is an abstract superclass of all actual implementations of the Abstract Window Toolkit (AWT). Subclasses of Toolkit are used to bind the various AWT components to particular native toolkit implementations.
Many components are affected if a display device, keyboard, or mouse is not supported. An appropriate class constructor throws a HeadlessException:
ButtonCheckboxChoiceDialogFileDialogFrameLabelListMenuMenuBarMenuItemPopupMenuScrollbarScrollPaneTextAreaTextFieldWindow
Such heavyweight components require a peer at the operating-system level, which cannot be guaranteed on headless machines.
Methods related to Canvas, Panel, and Image components do not need to throw a HeadlessException because these components can be given empty peers and treated as lightweight components.
The Headless toolkit also binds the Java technology components to the native resources, but it does so when resources don’t include a display device or input devices.
![]()
The java.awt.GraphicsEnvironment class is an abstract class that describes the collection of GraphicsDevice objects and Font objects available to a Java technology application on a particular platform. The resources in this GraphicsEnvironment might be local or on a remote machine. GraphicsDevice objects can be monitors, printers, or image buffers and are the destination of Graphics2D drawing methods. Each GraphicsDevice has many GraphicsConfiguration objects associated with it. These objects specify the different configurations in which the GraphicsDevice can be used.
Table 1 shows the GraphicsEnvironment methods that check for headless mode support.
Table 1. The Headless Mode Methods
![]()
Method
Description
public static boolean isHeadless()
Tests whether the environment is headless, and therefore does not support a display device, keyboard, or mouse. If this method returns true, a HeadlessException is thrown from areas of the Toolkit and GraphicsEnvironment classes that depend on a display device, keyboard, or mouse.
public boolean
isHeadlessInstance()
Returns whether this GraphicsEnvironment can support a display device, keyboard, or mouse. If this method returns true, aHeadlessException is thrown from areas of the GraphicsEnvironment that depend on a display device, keyboard, or mouse.
![]()
Note: The isHeadless() method checks the specific system property, java.awt.headless, instead of the system’s hardware configuration.
A HeadlessException is thrown when code that depends on a display device, keyboard, or mouse is called in an environment that does not support any of these. The exception is derived from an UnsupportedOperationException, which is itself derived from a RuntimeException.
Setting Up Headless Mode
![]()
To use headless mode operations, you must first understand how to check and set up the system properties related to this mode. In addition, you must understand how to create a default toolkit to use the headless implementation of the Toolkit class.
System Properties Setup
To set up headless mode, set the appropriate system property by using the setProperty() method. This method enables you to set the desired value for the system property that is indicated by the specific key.
System.setProperty("java.awt.headless", "true");
In this code, java.awt.headless is a system property, and true is a value that is assigned to it.
You can also use the following command line if you plan to run the same application in both a headless and a traditional environment:
java -Djava.awt.headless=true
Default Toolkit Creation
If a system property named java.awt.headless is set to true, then the headless implementation of Toolkit is used. Use the getDefaultToolkit() method of the Toolkit class to create an instance of headless toolkit:
Toolkit tk = Toolkit.getDefaultToolkit();
Headless Mode Check
To check the availability of headless mode, use the isHeadless() method of the GraphicsEnvironment class:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); boolean headless_check = ge.isHeadless();
This method checks the java.awt.headless system property. If this property has a true value, then a HeadlessException will be thrown from areas of the Toolkit andGraphicsEnvironment classes that are dependent on a display device, keyboard, or mouse.
完整文章见:http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/
留声机