我使用的是 linux 服务器,但出现如下错误:java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
我使用 java.awt.robot 类截屏
这是我的代码:
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class DesktopScreenshot {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
Rectangle screenShot = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenShot);
File screenshotFile = new File("screenshot.png");
ImageIO.write(screenFullImage, "png", screenshotFile);
}
}
设置无头属性:System.setProperty("java.awt.headless", "false") 然后我得到 java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
当我将此属性设置为 true 时,它会给出“无头环境异常”