1

I made an application in which I had some images to be loaded for icons. The program runs fine with every thing working and images loading as expected , when I was loading the images using

imageOpen = Toolkit.getDefaultToolkit().getImage("Images\\open.png");

But when I exporter it as jar the Images didn't show up Then after looking here and there I tried some things but in both cases the URL is returning null

URL u = this.getClass().getResource("Images\\open.png");
imageOpen = Toolkit.getDefaultToolkit().createImage(u);

Another case

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("Images\\open.png");
imageOpen = Toolkit.getDefaultToolkit().createImage(resource);

Can any one tell me why am I getting null , I even tried "\Images\open.png" ,"/Images/open.png" and ".\Images\open.png" for the path I am using eclipse and the images are stored in the parent directory in a folder named Images and the source files are in src>master

cornercoder
  • 276
  • 1
  • 4
  • 15
  • Are you under windows?. In your file system, windows ignore case letter. Inside the jar, java don´t ignore it. Do you have Images with upper case inside the jar? – Chuidiang Nov 01 '12 at 17:45
  • yes I have taken care of the case and have Images as it is in jar – cornercoder Nov 01 '12 at 17:47
  • Assuming Eclipse, did you make the Images folder a source folder (in addition to src being a source folder)? If you did this, it should be copying it to the class folder, which is where it will be looking for the actual image file. – martinez314 Nov 01 '12 at 20:26
  • I the jar file i have Images/open.png and master/Myclass.class. but why would it look in the class folder for images – cornercoder Nov 02 '12 at 01:02
  • @cornercoder : Please do have a look at this [answer](http://stackoverflow.com/a/9866659/1057230) of mine for help, and this [answer](http://stackoverflow.com/a/11372350/1057230) for further clarification, if you doing it manually. – nIcE cOw Nov 02 '12 at 02:11

3 Answers3

3

use images/open.png. The forward slash works on any OS. Also, jar tvf your jar file to make sure the images directory is capitalized Images as you believe it to be. It may not be, and if you're on Windows (or a case-insensitive filesystem), it may work fine from the directory but not from a jar.

dashrb
  • 952
  • 4
  • 10
  • tried it and the folder name is Images still getting the same exception Uncaught error fetching image: java.lang.NullPointerException at sun.awt.image.URLImageSource.getConnection(Unknown Source) at sun.awt.image.URLImageSource.getDecoder(Unknown Source) at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source) at sun.awt.image.ImageFetcher.fetchloop(Unknown Source) at sun.awt.image.ImageFetcher.run(Unknown Source) – cornercoder Nov 01 '12 at 17:50
  • 1
    Inside the jar file, do you have: `Images/open.png` and `com/your/pkg/YourClass.class` or is there some other directory layout? If possible, paste the output of `jar tvf Your.jar` – dashrb Nov 01 '12 at 18:25
  • I the jar file i have Images/open.png and master/Myclass.class.Can you explain what is jar tvf ? – cornercoder Nov 02 '12 at 01:00
  • *"Can you explain what is jar tvf ?"* `jar` is a command of the SDK. For `tvf` see the [jar tool documentation](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html). That output is important. – Andrew Thompson Nov 02 '12 at 04:38
  • Since you're using `this.getClass().getResource()`, I believe the path you're passing to `getResource()` is relative to the class: if you pass `Images/open.png`, then the `Images` directory must be below the directory containing `Myclass.class`, i.e. the jar should contain `master/Myclass.class` and `master/Images/open.png`. – dashrb Nov 07 '12 at 20:33
1

Are you sure url is null or does the problem come when using imageOpen?. Sometimes Toolkit.getDefaultToolkit().createImage() returns inmediatly but the image is not fully loaded and imageOpen is not usable. You need to wait for the image's load. This is the java code

JLabel label = new JLabel();
MediaTracker media = new MediaTracker(label);

Image imagen = Toolkit.getDefaultToolkit().getImage ("fichero.gif");
media.addImage(imagen, 23);
try
{
   media.waitForID(23);
}
catch (InterruptedException e)
{
   ...
}
Chuidiang
  • 1,055
  • 6
  • 13
  • 1
    Yes the URL is null , I printed the value of URL after calculating it and the problem only comes when I use the URL to create the image , get image seems to be working fine when launching the application from eclipse but does not work when I try it from its executable jar – cornercoder Nov 02 '12 at 00:58
0

I was able to achieve the solution to my problem with the help of a making resource bundle for the jar and taking the path from that file

cornercoder
  • 276
  • 1
  • 4
  • 15