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);
}
}