9

I am creating a screen capture using java.awt.Robot under Linux with OpenJDK 11. The result on Linux is a whole black image. The same code works on Ubuntu and Windows (using another file path of course).

Any clue?

public void captureScreen() throws AWTException {
    Robot robot = new Robot(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
    BufferedImage screen = robot.createScreenCapture(new Rectangle(getDefaultToolkit().getScreenSize()));
    try {
        ImageIO.write(screen, "jpg", new File("/tmp/screenshot.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

UPDATE: The the cause of the problem lies in the combination of OpenJDK and Wayland. With Oracle JDK/JRE (13.0.1) everything works fine.

Reto
  • 1,220
  • 15
  • 25
  • Works fine for me (OpenJDK 11.0.4 2019-07-16 on Ubuntu 18.04). – Robert Nov 23 '19 at 15:44
  • That's odd, I'm using OpenJDK 11.0.5 2019-10-15 on Debian 10 (Buster). So the problem could be related to the desktop environment? – Reto Nov 23 '19 at 18:54
  • 1
    I've tried on xubuntu, i.e., XFCE. I'd doubt that the desktop environment makes a difference. – Robert Nov 24 '19 at 03:35
  • Thanks for your effort! I suspected AppArmor as possible cause and uninstalled it, but it did not change anything. – Reto Nov 24 '19 at 08:48
  • #1 What do you mean with works in linux but not in ubuntu? #2 Could you try with some of these apps https://alternativeto.net/software/snagit/?platform=linux in order to discard or verify some issue or misconfiguration at o.s level? – JRichardsz Dec 30 '19 at 15:47
  • #1 it works on Ubuntu 18.04 but it does not work on a fresh installation of the latest Debian (10 aka Buster) #2 i'll give it a try this saturday - but I think the problem is rather Java related, as the default screenshot app works as expected. – Reto Dec 30 '19 at 20:38
  • 1
    Is it possible for some reason you have multiple graphic devices and Java is seeing one which isnt the "default"? Maybe try screenshots on all of `GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()` – ug_ Jan 03 '20 at 00:15
  • No, I've got only one integrated Intel GPU. – Reto Jan 04 '20 at 15:13

1 Answers1

3

If you are using wayland instead of XOrg this may be causing the problem as it is less stable with Java interfaces for graphics operations.

Edit: This bug has now been fixed (see OP)

James Stone
  • 108
  • 2
  • 7
  • You're perfectly right! I worked as expected as soon as is switched to Gnome on xorg. So the problem is definetly Wayland related, I opened an issue on their project site https://gitlab.freedesktop.org/wayland/wayland/issues/130 Thanks a lot! – Reto Jan 04 '20 at 16:27