0

this is my code,it works well on windows,by failed in ubuntu

void linuxShot() throws AWTException, IOException {
   Robot robot=new Robot();
   Dimension d=  Toolkit.getDefaultToolkit().getScreenSize();
   Rectangle screenRect=new  Rectangle(d);
   BufferedImage bufferedImage=robot.createScreenCapture(screenRect);
   //save picture
   File file=new File("temp"+File.separator+"screen.png");
   ImageIO.write(bufferedImage,"png",file);

}

there is my system infomation: jdk version:

openjdk version "1.8.0_352"
OpenJDK Runtime Environment Corretto-8.352.08.1 (build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM Corretto-8.352.08.1 (build 25.352-b08, mixed mode)

system :

PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

when i run this code in ubuntu,this is no exception,but the picture is all black,how can I resolve this problem or is there annother way to take a screenshot by java which can works on both windows and linux

test code:

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class Main {
    public static void main(String[] args) throws AWTException, IOException {
        Robot robot=new Robot();
        Dimension d=  Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRect=new  Rectangle(d);
        BufferedImage bufferedImage=robot.createScreenCapture(screenRect);
        //save picture
        File file=new File("screen.png");
        ImageIO.write(bufferedImage,"png",file);
    }
}
Joe
  • 1
  • 1
  • my current desktop is gnome – Joe Nov 16 '22 at 09:49
  • https://stackoverflow.com/questions/24668407/how-to-take-a-screenshot-of-desktop-fast-with-java-in-windows-ffmpeg-etc Maybe? – Grim Nov 16 '22 at 09:55
  • @Grim I do not want to use ffmpeg or java cv because i want to run this code on many other platforms , so i want to implement this function in java only – Joe Nov 16 '22 at 10:19
  • Works for me. Make sure you run it on the EDT – g00se Nov 16 '22 at 10:34
  • We don't know the context of how that code is executed. For example, is it just executed in the main() method of your class, or do you click on a button in the frame? Post a proper [me] demonstrating the problem. Then people can copy/paste/compile/test to see if they have the same problem. – camickr Nov 16 '22 at 15:37
  • @camickr i have re-edit the question , added test code at the bottom of the question – Joe Nov 17 '22 at 01:51
  • @g00se you can see the test code, it works on windows but not works on ubuntu, why i need edt – Joe Nov 17 '22 at 02:02
  • Again, usually an image is taken from a GUI when the application is running and the JVM is active. I would guess the issue is that the JVM closes before the image can be created on Linux. Try more realistic test. – camickr Nov 17 '22 at 03:40
  • @camickr i try the test code on centos , it works, the code should be ok – Joe Nov 17 '22 at 06:21

0 Answers0