Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 450
Shreyas Dave Avatar asked Nov 21 '12 05:11

Shreyas Dave


2 Answers

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.

like image 53
Shreyas Dave Avatar answered Nov 15 '22 08:11

Shreyas Dave


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.

like image 3
GingerHead Avatar answered Nov 15 '22 06:11

GingerHead