This code works only if I run my project, but if I run the Jar file, I get a NullPointerException. The image is in src directory.
try {
URL resource = Sticker.class.getResource("\\transparentSticker.png");
img = null;
this.img = ImageIO.read(Paths.get(resource.toURI()).toFile());
System.out.println("c");
} catch (IOException | URISyntaxException e) {
System.out.println("caught");
}
If I use this code, an IllegalArgumentException is being thrown.
InputStream input = Sticker.class.getResourceAsStream("\\transparentSticker.png");
if (input == null) {
input = Sticker.class.getClassLoader().getResourceAsStream("\\transparentSticker.png");
}
try {
img = ImageIO.read(input);
} catch (IOException e) {
e.printStackTrace();
}
How to load the image?