简体   繁体   中英

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 JMenuItem s (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.

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"))),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM