Question

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

Was it helpful?

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top