Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting java.awt.headless=true programmatically

Tags:

java

awt

headless

I'm trying to set java.awt.headless=true during the application startup but it appears like I'm too late and the non-headless mode has already started:

static {     System.setProperty("java.awt.headless", "true");     /* java.awt.GraphicsEnvironment.isHeadless() returns false */ } 

Is there another way set headless to true beside -Djava.awt.headless=true? I would prefer not configure anything on the console.

like image 210
reto Avatar asked Mar 31 '10 11:03

reto


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.

How do you handle headless exception in Java?

Since certain Java AWT components such as Frame and Button require peripheral devices, they will not work in the Java headless mode. If they are used in the headless mode, a HeadlessException is thrown. In the updated example above, the JFrame component is only used and displayed in a non-headless environment.


2 Answers

I was working with a main() in a class which statically loads different parts of JFreeChart in Constants (and other static code).

Moving the static loading block to the top of the class solved my problem.

This doesn't work:

  public class Foo() {     private static final Color COLOR_BACKGROUND = Color.WHITE;      static { /* too late ! */       System.setProperty("java.awt.headless", "true");       System.out.println(java.awt.GraphicsEnvironment.isHeadless());       /* ---> prints false */     }      public static void main() {}   } 

Have java execute the static block as early as possible by moving it to the top of the class!

  public class Foo() {     static { /* works fine! ! */       System.setProperty("java.awt.headless", "true");       System.out.println(java.awt.GraphicsEnvironment.isHeadless());       /* ---> prints true */     }      private static final Color COLOR_BACKGROUND = Color.WHITE;      public static void main() {}   } 

When thinking about it this makes perfectly sense :). Juhu!

like image 71
reto Avatar answered Sep 20 '22 03:09

reto


This should work because the call to System.setProperty is before the creation of the toolkit:

public static void main(String[] args) {     // Set system property.     // Call this BEFORE the toolkit has been initialized, that is,     // before Toolkit.getDefaultToolkit() has been called.     System.setProperty("java.awt.headless", "true");      // This triggers creation of the toolkit.     // Because java.awt.headless property is set to true, this      // will be an instance of headless toolkit.     Toolkit tk = Toolkit.getDefaultToolkit();      // Check whether the application is     // running in headless mode.     GraphicsEnvironment ge =          GraphicsEnvironment.getLocalGraphicsEnvironment();     System.out.println("Headless mode: " + ge.isHeadless()); } 
like image 34
pitpod Avatar answered Sep 19 '22 03:09

pitpod