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

3

12

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

Boehmenism answered 21/11, 2012 at 5:38 Comment(4)
Can you post the error or exceptionArgonaut
java.awt.AWTException: headless environment at Robot robot = new Robot();Boehmenism
What Java Version is the Mac running?Coel
java version "1.6.0_37" Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909) Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)Boehmenism
B
33

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.

Boehmenism answered 26/11, 2012 at 6:3 Comment(1)
did this work in headless environment ?Topheavy
A
3

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.

Argonaut answered 21/11, 2012 at 7:19 Comment(0)
K
3

You can simply add this line of code System.setProperty("java.awt.headless", "false"); before the Robot robot = new Robot();.

Keble answered 4/5, 2021 at 7:38 Comment(1)
This also solves the identical problem in Windows.Cassady

© 2022 - 2024 — McMap. All rights reserved.