Getting images from inside a jar

Trying to get the image from the jar resource.

Page 1 of 1

13 Replies - 2208 Views - Last Post: 21 November 2009 - 12:59 PM Rate Topic: -----

#1 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Getting images from inside a jar

Post icon  Posted 21 November 2009 - 10:15 AM

I'm working on my program running off a jar and not having any extra folders that i have to distribute (all files and images are inside the jar locally). Ive gotten everything else to work but images. I'm trying to call the image via:

Image img;
String path = getClass().getResource("image.png").getPath();
img = Toolkit.getDefaultToolkit().createImage(path);



When i run the jar, it errors out when:
String path = getClass().getResource("image.png").getPath();



is called... like the path is incorrect or something, and the image cannot be found. Do i have to use some other way or URL to access the image? I know its possible because i can access text files within a jar, so Images should really be no different. Any help is greatly appreciated and thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting images from inside a jar

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Getting images from inside a jar

Posted 21 November 2009 - 10:20 AM

If the image is at the root of the jar, I believe you have to add a /:

Image img;
String path = getClass().getResource("/image.png").getPath();
img = Toolkit.getDefaultToolkit().createImage(path);



If not, you have to indicate the full path within the jar.
Was This Post Helpful? 0
  • +
  • -

#3 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 10:26 AM

View PostEdwinNameless, on 21 Nov, 2009 - 09:20 AM, said:

If the image is at the root of the jar, I believe you have to add a /:

Image img;
String path = getClass().getResource("/image.png").getPath();
img = Toolkit.getDefaultToolkit().createImage(path);



If not, you have to indicate the full path within the jar.


yea, i hear ya, that was a sample. Here's one of the real paths my program is generating:

/C:/Documents%20and%20Settings/Bla/Desktop/Project/Project%20Engine%20B.0.3/src/ProjectEngineB03/Image/Rose/rose.png

This is what I'm actually getting via my real code when i call
String path = getClass().getResource(str).getPath();



str being: Image/Rose/rose.png or something along those lines.
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Getting images from inside a jar

Posted 21 November 2009 - 10:42 AM

So is the slash making any differnce?
Was This Post Helpful? 0
  • +
  • -

#5 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 10:50 AM

View PostEdwinNameless, on 21 Nov, 2009 - 09:42 AM, said:

So is the slash making any differnce?


lol no, it's not.. I even tried using:

String str = "Rose/rose.png";
Image img;
InputStream is = getClass().getResourceAsStream(str);
img = ImageIO.read(is);



but the same thing happens... error at:

img = ImageIO.read(is);



Saying null pointer exception because it cant find an image... The path is correct. I figured I'd try an InputStream because if you are trying to read a text file inside a jar, you do:

InputStream isL = getClass().getResourceAsStream("test.txt");
InputStreamReader isrL = new InputStreamReader(isL);
BufferedReader leftDoc = new BufferedReader(isrL);



And you know.... the text code works fine, but yea, it just wont seem to find the images worth my life....
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:07 PM

If on windows, you have to use the backslash (correct me if I'm wrong) and when you use the backslash you have to use two where you would normally use one (because in a string \ is an escape character). So the string could look like this on wiondows:

String str = "images\\pic.png";



If on Linux, just use a single forward slash to denote the directory.
Was This Post Helpful? 0
  • +
  • -

#7 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:14 PM

View PostDogstopper, on 21 Nov, 2009 - 11:07 AM, said:

If on windows, you have to use the backslash (correct me if I'm wrong) and when you use the backslash you have to use two where you would normally use one (because in a string \ is an escape character). So the string could look like this on wiondows:

String str = "images\\pic.png";



If on Linux, just use a single forward slash to denote the directory.


yea. you are kindof incorrect. its true if you use: \ then you need \\, however if you use / you only need one, but thats not the issue here, at least i think its not.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:16 PM

View Postpainkiller102, on 21 Nov, 2009 - 01:14 PM, said:

yea. you are kindof incorrect. its true if you use: \ then you need \\, however if you use / you only need one, but thats not the issue here, at least i think its not.


That's what I said....But I may be making a broad assumption here that Java is not that OS independent (At least in that matter). I do know that the File object does have an OS independent solution found under the File.separator value. (You may need to instantiate a file to use...

This post has been edited by Dogstopper: 21 November 2009 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:22 PM

And I quote:

Quote

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\" (back slash) while Linux use "/" (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.


From here:
http://www.kodejava....amples/325.html
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:32 PM

Not sure it is a Path() that you need from a .jar... I might be wrong ....

I always do:

URL urlImage = getClass().getResource("images/image.png");
ImageIcon icon = new ImageIcon(urlImage);

Also make sure you are doing your getClass() in a class that is at the root of your package
if you have
ca.pblinc.myApp
ca.pblinc.myApp.images
ca.pblinc.myApp.gui

you cannot do your getClass() in a class that is in ca.pblinc.myApp.gui

*Edited: to add the getClass() issue

This post has been edited by pbl: 21 November 2009 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#11 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:32 PM

View PostDogstopper, on 21 Nov, 2009 - 11:22 AM, said:

And I quote:

Quote

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\" (back slash) while Linux use "/" (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.


From here:
http://www.kodejava....amples/325.html


As i said, the backslahes isnt the issue here, When i run the program in netbeans its fine, when i execute its jar, its not. Im not retrieving the right path to the images inside of the jar somehow. If my slashes were wrong it wouldnt get the image period even in Netbeans
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:36 PM

Oh...I'm so sorry I misunderstood you...pbl looks like he has a great solution.
Was This Post Helpful? 0
  • +
  • -

#13 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:49 PM

Yea.. the issue is still there even with imageicons... something is going terribly wrong XD so i guess ill just do independent testing until i figure it out. its gotta be a URL issue somewhere in there. But thanks for all of the reply's!
Was This Post Helpful? 0
  • +
  • -

#14 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Getting images from inside a jar

Posted 21 November 2009 - 12:59 PM

OK I SOLVED IT! no ones gonna believe this, however the URL was not the issue at all. Apparently i had to call:
String src = "Rose/rose.PNG";
Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(src);



instead of:

String src = "Rose/rose.PNG";
Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource(src);



I'm not entirely sure why this was an issue, but once i changed it to getImage it suddenly worked!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1