I am trying to automate screenshots, among other things. To achieve this, I have a bash script that does the other work and then invokes my jar using the java -jar
command.
In my Java code, I am using java.awt.Robot to capture a screenshot. Essentially, this bit is my code.
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("C:\\Screenshot\\CurrentScreenshot.png"));
This works fine when I run the Java code on my local IDE, and captures the screenshot. However, when I package my jar, deploy it, and run my bash script it only captures a blank screenshot that is entirely grey in color. The dimensions of the image are full-screen (136x768). Not sure why this happens.
I connect to my server via PuTTY, so this might be due to that maybe? From the help I could gather on the internet, I installed Xming on my machine (a VM running Windows 7), and started Xserver. I configured PuTTY to allow X11 Forwarding. But I still couldn't get the screenshot to work. (Could my configuration here be a potential problem?)
I also tried using this piece of code, which surprisingly did not work for me.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_PRINTSCREEN);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_ALT);
I can't use ImageMagick or such due to restrictions at my workplace.
Any ideas on why I am getting a grey screenshot and how to resolve it?