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.

:flushed: I’m so sorry! I found all my screenshots :upside_down_face: They also worked on eos3.2 SDK. And the canberra module error message appears only once on first screenshot (attempt to an audio notification) when the program is still running (no crash exceptions). I found out this by implementing logging on every programm string. :crazy_face:
The next problem is that I was unable to set the program at system startup. I put the startup command in the bash script scr.sh

#!/bin/bash
flatpak run --filesystem=home --socket=x11 --command=/usr/lib/sdk/openjdk/bin/java org.freedesktop.Sdk -jar Desktop.Monitor/Screenshoter.jar

And then copied scr.sh to init.d folder

user@endless1:~$ sudo cp scr.sh /etc/init.d/scr.sh

Made them executable

user@endless1:~$ chmod ugo+x scr.sh

linked to the script for the desired startup mode (5th)

user@endless1:~$ ln -s /etc/init.d/scr.sh /etc/rc5.d/

However, autorun does not occur at the system startup… :slightly_frowning_face:
Is this a limitation of the Endless OS or my mistake?
Thank you for support!

UPD:


Anydesk not starting too.

This doesn’t work that way, as:

  1. EOS (like any other modern Linux) uses systemd instead of classical SysV init, so the init.d scripts shouldn’t be used anymore (even if they are still supported in some quirky way by systemd for compatibility reasons)
  2. You need to make sure that at execution time, X already has been brought up, so instead you should rely on GDM Autostart files (https://help.gnome.org/admin/gdm/stable/configuration.html.en#autostart)
    2.1 The directories mentioned in the above documentation are read-only on EOS due to it’s architecture, so you need another way
    2.2. This way is probably using some systemd unit which has a dependency on gdm (haven’t tested this out yet)

Can you please tell us more abou the desired solution you want to implement? What’s the goal? Eventually there are other ways of accomplishing it.

Thank you for your desire to help! The goal is to control the staff. I need to take screenshots at a specified frequency after the system boots.
Due to the restrictions on autorun, periodical screenshots in the chrome browser using the extension are also suitable. But google chrome webstore contains extensions that take one screenshot at the user’s command. Or I didn’t search well.
Apparently I should master javascript to write own extension? Perhaps there are other solutions?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.