What is the benefit of setting java.awt.headless=true?

There is no performance benefit of setting java.awt.headless=true if you're not using AWT features. AWT features are loaded on-demand.

As explained in the linked article, headless mode is useful for accessing some Java graphics features which are normally delegated to the graphics host:

After setting up headless mode and creating an instance of the headless toolkit, your application can perform the following operations:

  • Create lightweight components such as Canvas, Panel, and Swing components, except the top levels
  • Obtain information about available fonts, font metrics, and font settings
  • Set color for rendering text and graphics
  • Create and obtain images and prepare images for rendering
  • Print using java.awt.PrintJob, java.awt.print.*, and javax.print.* classes
  • Emit an audio beep

For example, in headless mode you can create and write image files:

    BufferedImage img = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();
    g.drawLine(80, 30, 120, 70);
    g.drawLine(80, 70, 120, 30);
    ImageIO.write(img, "png", new File("image.png"));

When run with -Djava.awt.headless=true, will produce an image file:

x

When run with -Djava.awt.headless=false (and without an X window server) will throw an exception instead:

java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.

Note that the JVM contains heuristics that determine the value of java.awt.headless if it's not explicitly set. For example, on Linux if the DISPLAY environment variable is not set, java.awt.headless automatically becomes true.


Headless and non-headless modes are different, they have different set of features. If you only need to do some simple things like font rendering then yes, you will be able to do it in headless mode.

You can always check the guts of the JDK sources and see for yourself, what methods are dependent on non-headless mode. But in my opinion, even if the performance gain is negligible, it's best to pass java.awt.headless anyway (if you do not need "full" GUI mode).

Any vendor can use this property. You never know if they are going to do something if you have the full GUI. So, my rule of thumb is: always use java.awt.headless for the console apps and the servers. It won't harm.


One possible benefit is that if you are invoking the application while trying to do something else in a window perhaps invoking the application multiple times, it will not disrupt your keyboard/mouse focus if the application runs in headless mode.

At least on a Mac I have had huge problems running a script which repeatedly runs a java app every few seconds while trying to edit in another window. Headless mode fixes that.