Accessing Images in JAR file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 25
Reputation: Black Knight is an unknown quantity at this point 
Solved Threads: 0
Black Knight's Avatar
Black Knight Black Knight is offline Offline
Light Poster

Accessing Images in JAR file

 
0
  #1
Apr 14th, 2005
Right, my program ran perfectly fine with the line
  1. JLabel headerImage = new JLabel(new ImageIcon(img));
But as soon as i put it into a JAR file it has problems, i have searched the inet for soloutions and there isnt much out there for apps, loats for applets though, but none of the solutions seem to work. I currently have got this to work in the app but still wont work when i jar it up.
  1. Image img = null;
  2. try {
  3. InputStream in = new InputStream(getClass().getResourceAsStream("./com/images/loginTitle.jpg"));
  4.  
  5. // Read from an input stream
  6. img = ImageIO.read(in);
  7.  
  8. } catch (Exception e1) {
  9. e1.printStackTrace();
  10. }
  11. JLabel headerImage = new JLabel(new ImageIcon(img));

Surly there is a way to access images stored within a jar file, they aren not in a seperate jar in the jar . They are simply in a package in the main JAR.

Any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Accessing Images in JAR file

 
0
  #2
Apr 14th, 2005
I had the same problem a few months ago. The best thing to do is to make a URL like this:

URL imageURL = JARFILENAME.class.getResource("IMAGE.JPG");

Just replace the colored parts to your own stuff...

then...

construct the image like so...

  1. Image myImage = Toolkit.getDefaultToolkit().getImage(imageURL);
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Accessing Images in JAR file

 
0
  #3
Apr 14th, 2005
Yes, the URL Object should make that work.

Knight, is this JAR file an executable JAR file? If it is, could you show me how you did it? I tried once and never could get mine to work, even though everything was correct.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 25
Reputation: Black Knight is an unknown quantity at this point 
Solved Threads: 0
Black Knight's Avatar
Black Knight Black Knight is offline Offline
Light Poster

Re: Accessing Images in JAR file

 
0
  #4
Apr 18th, 2005
Im using eclise and exporting it as a jar file. Its fairly easy to do.

The URL thing doesnt work, I am not sure how it is supposed to work anyway, how is it supposed to access a jar file that isnt created until the code is done?, it just gives me these errors :

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)

Which is understandable as there is no connection etc as this isnt an applet. Please corrct me if im way off the mark but it should work before i JAR it up shouldnt it, it wont compile otherwise. It is all going in one Executable JAR file which is a stndalone application, not a web applet. the images are within that JAR file along with all the other class files. I cant access the JAR file from within the code as I havent created it yet. And i cant create it until it compiles and it wont compile as it is trying to reference things in the JAR which isnt created yet......


I just need to access images from with my jar file...... why do jar files create so much trouble :cry:
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Accessing Images in JAR file

 
0
  #5
Apr 18th, 2005
with the URL code, it's looking for the images inside of the Jar file, obviousely, if there is no jar file created, then it won't be able to find the images, so it must be created before you try and run the code...
Create a jar file and make sure that the images you want to call are in the jar file. Also, save a text file called 'manifest.txt' into the jar file. 'manifest.txt' should look like this:

Main-Class: (the name of the class that holds your main method)
Make sure that you have a carriage return after that as well.

When you go to compile the jar file, just type in this...:

%jar -cvmf manifest.txt (create name for jar file here) *.class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC