javamacosjavafx-2headlessawtrobot

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


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();


Solution

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