Ky -
Ky -

Reputation: 32173

Accessing images in a .JAR file

I have a set of 5 images in my program. The program will be compiled as a single .JAR file, so they need to be read from it as such. However, when I make a call like

images = new ArrayPP<ImageIcon>(
          new ImageIcon(getClass().getResource("/bha/resources/Portal Test Chamber 17 - Part 1.png")),
          new ImageIcon(getClass().getResource("/bha/resources/Portal Test Chamber 17 - Part 2.png")),
          new ImageIcon(getClass().getResource("/bha/resources/Portal Test Chamber 17 - Part 3.png")),
          new ImageIcon(getClass().getResource("/bha/resources/revit logo - glass.png")),
          new ImageIcon(getClass().getResource("/bha/resources/The All-Knowing Octopus.png")));

which works for the icons in JMenuItems (for example, jMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bha/resources/icon.png"))); will always work) and when testing in an IDE, but returns as null when compiled as a JAR file.

Upvotes: 1

Views: 1375

Answers (1)

MeBigFatGuy
MeBigFatGuy

Reputation: 28608

Given that getResource returns a URL, i'm guessing that it's not liking the spaces. As a test, i'd try doing

new ImageIcon(getClass().getResource(URLEncoder.encode("/bha/resources/Portal Test Chamber 17 - Part 1.png", "UTF-8"))),

Upvotes: 1

Related Questions