10

I am trying to load icons from a jar file. I have both tried to load it from classes within the jar file as well as classes outside the jar file.

outside of the jarfile - returned a null exception

java.net.URL imageURL = LoadHTMLExample.class.getClassLoader()
    .getResource("icons/mouse.png");

in side of the jar file in the LoadHTMLExample

java.net.URL imageURL = this.getClass().getClassLoader()
    .getResource("icons/mouse.png");

get the same error.

I have also tried variations of "icons", "/icons" "icons/" "/icons/mouse.png" "icons/mouse.png"

nothing seems to work any idea

the icon is in the jar file

jar
 --icons --- {all the images}

 --com.blah.blah

6 Answers6

6

I've always used the system class loader, whose path is relative to the root of the JAR:

URL url = ClassLoader.getSystemClassLoader().getResource("icons/mouse.png");
Icon icon = new ImageIcon(url);
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
5

Skip the class loader, and get the resource as a stream instead. If you don't need the URL you can turn them directly into BufferedImages like so. I've left the stream and exception handling as a further exercise.

InputStream stream = LoadHTMLExample.class
    .getResourceAsStream( "/icons/mouse.png" );
BufferedImage image = ImageIO.read( stream );

Questioner needs the URL which brings us back to everyone else's suggestions. The images are definitely in the jar aren't they?

banjollity
  • 4,490
  • 2
  • 29
  • 32
  • Class.getResourceAsStream() just delegates to the ClassLoader. It's essentially the same thing. – skaffman Jul 15 '09 at 18:39
  • I need the URL, a webrenderobject is going to use the resource location to pull the icons –  Jul 15 '09 at 19:08
  • also tried you method, and image keeps is always null, so either way isn't working...what is blocking the reading of a jar file? –  Jul 15 '09 at 19:56
1

I think that getResource gets the resource relative to the location of LoadHTMLExample.class. So your jarfile should be structured in the following way:

myjar.jar
 |
 |- ...
 |- LoadHTMLExample.class
 |- ...
 \-- icons
      |
      \- mourse.png

Also, you might be getting stream through getResourceAsStream than getting the URL.

notnoop
  • 58,763
  • 21
  • 123
  • 144
  • 1
    Alternatively, change the path from *icons/mourse.png* to */icons/mourse.png* (note the '/' at the beginning). – Peter Štibraný Jul 15 '09 at 18:37
  • Everything else considered, you definitely need to do what Peter suggested, because to load "icons/mouse.png" means to load it relative to the class. In your JAR you have icons off the root, so you must prefix with a foreslash. – Dan Gravell Jul 15 '09 at 19:42
  • 1
    I got it to work! final java.net.URL imageURL3 = com.java.html.LoadHTMLExample.class.getResource( "icons/" ); that works for the above diagram –  Jul 15 '09 at 21:48
0

Is the jar in question on the classpath of your runtime? I have a jar with PNGs in it, and I can recreate the nulls if I don't include it on the classpath. If the jar is there they go away.

banjollity
  • 4,490
  • 2
  • 29
  • 32
0

final java.net.URL imageURL3 = com.java.html.LoadHTMLExample.class.getResource( "icons/" );

works for the below directory structure

myjar.jar | |- ... |- LoadHTMLExample.class |- ... -- icons | - mourse.png

Thanks for the help everyone

0
Icon icon = new ImageIcon(ClassLoader.getSystemResource("icons/mouse.png"));

From the JavaDoc of getSystemResource():

Find a resource of the specified name from the search path used to load classes...

This will find your icons even in a jar-file.

Adam Taras
  • 1,403
  • 1
  • 13
  • 15