0

I am using linux server but i get error like : java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

I take a screenshot using java.awt.robot class

Here is my code :

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

Set headless Property : System.setProperty("java.awt.headless", "false") then i get java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

and when i set this Property this to true it gives "headless environment exception"

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

0 Answers0