944,049 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 36463
  • Java RSS
Apr 14th, 2005
0

Accessing Images in JAR file

Expand Post »
Right, my program ran perfectly fine with the line
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Black Knight is offline Offline
25 posts
since Mar 2005
Apr 14th, 2005
0

Re: Accessing Images in JAR file

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...

Java Syntax (Toggle Plain Text)
  1. Image myImage = Toolkit.getDefaultToolkit().getImage(imageURL);
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Apr 14th, 2005
0

Re: Accessing Images in JAR file

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 18th, 2005
0

Re: Accessing Images in JAR file

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:
Reputation Points: 10
Solved Threads: 0
Light Poster
Black Knight is offline Offline
25 posts
since Mar 2005
Apr 18th, 2005
0

Re: Accessing Images in JAR file

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
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Executable?
Next Thread in Java Forum Timeline: JProgressBar while file copying





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC