Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 503
user2250246 Avatar asked Dec 09 '16 19:12

user2250246


People also ask

What is Java AWT headless true?

This means that your Java application does not display windows or dialog boxes, does not accept keyboard or mouse input, and does not use any heavyweight AWT components. This mode is selected by specifying Java property java. awt. headless=true on the Java invocation.

What is headless in Java?

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.


2 Answers

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.

like image 159
rustyx Avatar answered Sep 18 '22 14:09

rustyx


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.

like image 38
BaRoN Avatar answered Sep 21 '22 14:09

BaRoN