Taking a screenshot

Good afternoon everyone!
I’m successfully run java jar application by instructuions given here:

But getting a screenshot with the Robot class https://docs.oracle.com/javase/8/docs/api/java/awt/Robot.html gives an error:

No X11 DISPLAY variable was set, but this program performed an operation which requires it.

In other linux distributions, my code works well.

What method can I use to get a screenshot of the screen in Endless OS?
I will be very grateful for any help.

Is there anybody? :roll_eyes:

I am probably not completely sure what you are specifically looking to do. But to print the entire screen, I just use the “Print Scr” key on my keyboard. That captures the whole screen and automatically saves it to the Pictures folder in your home directory.

Add --socket=x11 to the flatpak run arguments.

@wjt Thank you for the reply! I still have Exception. Please check out resulting output:

user@endless1:~$ flatpak run --filesystem=host --command=java com.endlessm.Sdk/x86_64/eos3.2 -Djava.awt.headless=true -version
openjdk version “1.8.0_131”
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-1endless1bem1-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
user@endless1:~$ flatpak run --socket=x11 --filesystem=host --command=java com.endlessm.Sdk/x86_64/eos3.2 -jar -Djava.awt.headless=true /home/user/Desktop.Monitor/Screenshoter.jar
java.awt.HeadlessException
at sun.java2d.HeadlessGraphicsEnvironment.getScreenDevices(HeadlessGraphicsEnvironment.java:72)
at screener.robo(screener.java:17)
at screener.main(screener.java:63)

Thank you for the reply! I am just trying to automate screenshots

I am not at my Endless computer to check right now, but I believe you can use gnome-screenshot from the command-line to take a screenshot.

I am not a Java expert but I would guess that telling AWT to operate in headless mode is designed to make it work without touching/needing a real display, in which case it may not be surprising that it can’t take a screenshot of the real display.

@wjt thank you for the reply! I still can’t take a screenshot :frowning: You are right. Enabling headless mode is wrong. But in this case nothing is happens. Just flashing cursor in the terminal…
gnome-screenshot has non-disabling animation. And that’s a slightly different story to call terminal commands in Java.

I searched the app center for other screenshot apps. I found ksnip which seems to work OK and did not flash the screen:

wjt@camille:~$ flatpak run org.ksnip.ksnip --fullscreen --save
Warning: QSocketNotifier: Can only be used with threads started with QThread
Info: Image Saved: Saved to /sysroot/home/wjt/ksnip_20210914-194547.png

Or do you specifically need a Java API to take a screenshot? If you are working on a Java app I would personally not use the eos3.2 runtime but instead org.freedesktop.Sdk.Extension.openjdk

Java Screenshot Demo

Install the SDK and OpenJDK Extension

sudo flatpak install -y org.freedesktop.Sdk//21.08 org.freedesktop.Sdk.Extension.openjdk//21.08

Build the Demo application

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class test001
{
    public static void main(String[] args) throws AWTException,IOException
    {
        BufferedImage image=(new Robot()).createScreenCapture(
            new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        
        ImageIO.write(image,"jpg",new File("screenshot.jpg"));
    }
}

(i assume you have put the source code provided into a file called test001.java)

flatpak run --filesystem=home --command=/usr/lib/sdk/openjdk/bin/javac org.freedesktop.Sdk test001.java

Run the Demo

flatpak run --filesystem=home --socket=x11 --command=/usr/lib/sdk/openjdk/bin/java org.freedesktop.Sdk test001

This will capture a Screenshot and write it to the same location as the class file. Please also note, that taking a screenshot currently only works with X11 not with Wayland

1 Like

Thank you all for help! According to your commands SDK and OpenJDK Extension have been installed successfully:

user@endless1:~$ flatpak run --filesystem=home --command=/usr/lib/sdk/openjdk/bin/java org.freedesktop.Sdk -version
openjdk version “16.0.1” 2021-04-20
OpenJDK Runtime Environment 21.3 (build 16.0.1+9)
OpenJDK 64-Bit Server VM 21.3 (build 16.0.1+9, mixed mode, sharing)

But my Jar and demo code gets the same error with double output:

user@endless1:~$ flatpak run --filesystem=home --socket=x11 --command=/usr/lib/sdk/openjdk/bin/java org.freedesktop.Sdk test001
Gtk-Message: 16:27:15.630: Failed to load module “canberra-gtk-module”
Gtk-Message: 16:27:15.630: Failed to load module “canberra-gtk-module”

I tried to install the module with the command user@endless1:~$ sudo flatpak install -y ibcanberra-gtk-module that was not successful.

Do you know from which repository I can install the missing module?
The current repositories are presented below:

user@endless1:~$ flatpak remotes
Name Parameters
eos-apps system
eos-runtimes system
eos-sdk system
flathub system

Addition:
The link says that

The warning about canberra-gtk-module is because the gtk module is installed on your system, but isn’t available in flatpak. I also noticed it only happens on X11, and not on wayland.

I tried to remove the module on the advice, but it was not installed:

user@endless1:~$ sudo flatpak remove libcanberra-gtk-module
error: libcanberra-gtk-module/unspecified/unspecified not installed

Ignore the canberra error. It’s thrown because the module is not available in the Runtime used by the application in question - in a Flatpak environment, everything need to be available either directly in the flatpak or in the runtime.

You can’t install it seperatly like in Ubuntu per Design.

The canberra library is sometimes used by GTK applications to provide playback of event sounds as a fallback method, so it shouldn’t be a issue if it’s not there.