0

I have a fairly simple Java program going with the following structure:

/src/main/java/  (all the classes in here)
/src/main/resources  (marked as resource root in IntelliJ, one png file in here)
/pom.xml

The way I build my Maven project is a bit of a workaround (because i couldn't get IntelliJ's artifact system to work). I have a run/debug configuration that builds the project using Command line: install (hope I'm explaining that sufficiently well).

Everything works fine, except for the inclusion of the .png file. The weird thing is that the file is actually added to the executable jar, but somehow the final program doesn't appear to use it at all. It's accessed via:

BufferedImage image = ImageIO.read(new File(getClass().getResource(url).toURI()));

Again, the final program works perfectly fine, except that the image is not visible at any point. Any help?

Edit: The png is located in the root folder of the jar, and the supplied URL is a string which is simply the name of the png file. I should probably add that the program works fine in the IntelliJ-Run-environment (including the png), but not using the executable jar.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
BlueTooth4269
  • 113
  • 3
  • 14

1 Answers1

1

Found the answer that worked for me here: Access .png image in .jar file and use it

Had to use

img = ImageIO.read(MapObject.class.getResource(url));

instead of

ImageIO.read(new File(getClass().getResource(url).toURI()));

since the image was located in a jar.

Community
  • 1
  • 1
BlueTooth4269
  • 113
  • 3
  • 14
  • 1
    Ah! It was the toURI() that was messing things up. Generally `getResourceAsStream()` will avoid issues like that. – Samuel May 02 '16 at 21:08