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

35,328

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

Headless mode is mainly useful in those systems that don't have a graphical display, typically the servers.

Many applications use graphical displays to do things that are not necessarily needed to be seen, for instance drawing an image and then saving it to disk.

if you run such a program on a server (ssh connections only, no graphic endpoint) you get an exception in normal mode, you get the program ran in headless mode. Headless mode essentially means virtual display, the graphical components do their operations on a generic/transparent display interface (eg, they draw a circle on a grid), then the result is actually displayed in non-headless mode, or is treated differently in headless mode (eg, the grid is changed so that it would represent the drawn circle on a real display and such grid is kept in memory for purposes like file saving).

Share:
35,328
user2250246
Author by

user2250246

Updated on July 27, 2022

Comments

  • user2250246
    user2250246 almost 2 years

    I have gone through

    1. Setting java.awt.headless=true programmatically
    2. http://www.oracle.com/technetwork/articles/javase/headless-136834.html and
    3. Some other links too.

    Nowhere it is explained the benefit of using this flag.

    Is it a performance benefit? If yes, is there even a rough quntization how much performance benefit there will be? (I know that answers to performance questions totally depend upon case to case, but it would be nice to know if someone reported a good benefit from doing this).

  • nanosoft
    nanosoft over 5 years
    This answer in NO way tells the benefit of setting headless property - So the original answer is UNANSWERED. I am also looking for answer and yet to get one.
  • BaRoN
    BaRoN over 5 years
    There are many different Java VM's, each one can interpret the flag in different way. You can check the OpenJDK sources to find every reference. Most common use is just using: if (GraphicsEnvironment.isHeadless()) { throw new HeadlessException(); } Font rendering and image processing code is slightly different. Also, any application may rely on this flag. For example, previously, tomcat was having crashes when flag wasn't used and tomcat was ran from system scripts.