3

I'm trying to take a screenshot of my window with the following command.

BufferedImage screenCapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenCapture, "jpg", "data/file.jpg");

I then use this image and send it to my discord server

This works fine, it saves the file to the dir data/file.jpg and sends it to my server but when I make the file into a runnable .jar file, it doesn't work anymore


UPADTE I'm streaming it now -

I decided to stream the image, again it works when I run it through intellij, but when its a jar runnable its not working


BufferedImage screenCapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(screenCapture, "jpg", stream);

Abdul
  • 73
  • 6

1 Answers1

1

Is the "data" directory inside your jar?, if that is your case, I believe you can not write/create files inside a jar file.

Check this link: Java - Writing to txt in a JAR file

You will have to save the file/image maybe in a relative path where the jar is, or in another location of your pc.

LuisRococo
  • 173
  • 1
  • 12