Headless environment error in java.awt.Robot class with MAC OS

14,003

Solution 1

This is to answer my own question, after searching many resources.

I have used following code to disable headless environment, and the problem is solved.

static {

        System.setProperty("java.awt.headless", "false");
}

Thanks.

Solution 2

From their API I can see the following:

  1. The constructors of Applet and all heavyweight components (*) are changed to throw HeadlessException if a display, keyboard, and mouse are not supported by the toolkit implementation
  2. The Robot constructor throws an AWTException if a display, keyboard, and mouse are not supported by the toolkit implementation
  3. Many of the methods in Toolkit and GraphicsEnvironment, with the exception of fonts, imaging, and printing, are changed to throw HeadlessException if a display, keyboard, and mouse are not supported
  4. Other methods that may be affected by lack of display, keyboard, or mouse support, are changed to throw HeadlessException
  5. It should be worth noting that the HeadlessException is thrown if and only if isHeadless returns true, and that all javadoc comments should specify this

So you need to check your hardware and their drivers.

Share:
14,003
Shreyas Dave
Author by

Shreyas Dave

A Logical, Creative and Enthusiastic person. Have excellent knowledge of Java.

Updated on June 05, 2022

Comments

  • Shreyas Dave
    Shreyas Dave almost 2 years

    I am trying to capture screen shots in my JavaFX application using Robot class,

    this is the code which I used in my application:

    Rectangle screenBounds = new Rectangle(Screen.getPrimary().getBounds().getWidth(),
               Screen.getPrimary().getBounds().getHeight());
    
    Robot robot = new Robot();
    
    BufferedImage img = robot.createScreenCapture(new java.awt.Rectangle(
         (int) screenBounds.getX(), (int) screenBounds.getY(), (int) 
                 screenBounds.getWidth(), (int) screenBounds.getHeight()));
    

    It is working perfectly in windows operating system, but showing an error of headless environment in MAC OS at Robot robot = new Robot();

  • Phani Kiran Gutha
    Phani Kiran Gutha over 2 years
    did this work in headless environment ?