JavaFX screencapture headless exception on OSX

JavaFX doesn’t use AWT stack, so it’s not being started in pure JavaFX application. Due to threads handling specifics AWT is being run in headless mode on Mac then requested from JavaFX.

There are next options to solve that:

  1. Use some voodoo magic to initialize AWT — in static initialization run java.awt.Toolkit.getDefaultToolkit(); EDIT this worked only in older JavaFX, sorry

  2. Better options would be to opt out of using AWT from JavaFX. You can use next functionality to make screenshots: http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#snapshot%28javafx.util.Callback,%20javafx.scene.SnapshotParameters,%20javafx.scene.image.WritableImage%29

  3. EDIT As Alexander pointed out another way is to run AWT code in a separate VM. To achieve that you can refactor your screenshot functionality to a separate class and call it from JavaFX app by:

        new ProcessBuilder(
              System.getProperty("java.home") + "/bin/java", 
              "-cp", "classpath", 
              "my.apps.DoScreenshot"
        ).start();
    

    This app can store screenshot to a filesystem.
    If you need to do screenshots often and met performance issues you can run that separate app once and communicate with it through socket.

  4. Use com.sun.glass.ui.Robot instead of AWTRobot

Leave a Comment