| | |
Accessing Images in JAR file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Right, my program ran perfectly fine with the line
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.
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?
Java Syntax (Toggle Plain Text)
JLabel headerImage = new JLabel(new ImageIcon(img));
Java Syntax (Toggle Plain Text)
Image img = null; try { InputStream in = new InputStream(getClass().getResourceAsStream("./com/images/loginTitle.jpg")); // Read from an input stream img = ImageIO.read(in); } catch (Exception e1) { e1.printStackTrace(); } 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?
I had the same problem a few months ago. The best thing to do is to make a URL like this:
Just replace the colored parts to your own stuff...
then...
construct the image like so...
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)
Image myImage = Toolkit.getDefaultToolkit().getImage(imageURL);
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:
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:
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:
Make sure that you have a carriage return after that as well.
When you go to compile the jar file, just type in this...:
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)When you go to compile the jar file, just type in this...:
%jar -cvmf manifest.txt (create name for jar file here) *.class![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: converting from javascript to HTML?
- Next Thread: JProgressBar while file copying
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class classes client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






