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

4

51

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).

Ramin answered 9/12, 2016 at 19:3 Comment(4)
why the minus vote?Ramin
If your application will run properly in "headless" mode, then it will not experience any performance difference regardless of the mode. Some graphics operations require "headed" mode and then you have no choice. Headless mode will prevent your application from doing things like popping-up windows on the console (if there is one).Clock
Possible duplicate of Meaning of headless and -D option in JAVA_OPTSAssoil
stackoverflow.com/questions/2552371Kushner
A
29

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.

Ault answered 23/1, 2020 at 16:58 Comment(0)
S
9

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.

Syrupy answered 9/12, 2016 at 19:13 Comment(2)
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.Kep
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.Syrupy
K
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 when in default mode, while you get the program ran when you enable the 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 either actually displayed, when in headed mode, or it is treated differently in headless mode, eg, the grid is a memory object, which is changed so that it would represent the drawn circle on a real display, the same grid can be used for tasks like saving everything as an image file.

As suggested by one of the comments, Oracle has a number of details about it.

Kory answered 30/11, 2020 at 2:31 Comment(1)
The only thing to add to this answer is the quote from the article mentioned in the question: Headless mode is a system configuration in which the display device, keyboard, or mouse is lackingLevitical
I
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.

Inspiration answered 17/3, 2019 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.