| | |
Adding images to JAR file (Netbean 5.0)
![]() |
Hello....
I'm currently using Netbean 5.0 and I just want to add some GIF files to my program when it builds. I'm using the files as icons for some of the GUI buttons and labels. But when I select clean and build main project or build main project and execute the JAR file outside the IDE, the icons are missing.
I put the image files into my project folder and basically just add this kinda code in.
So..I'm wondering if anyone can help me with this....
Thanks in advance.
I'm currently using Netbean 5.0 and I just want to add some GIF files to my program when it builds. I'm using the files as icons for some of the GUI buttons and labels. But when I select clean and build main project or build main project and execute the JAR file outside the IDE, the icons are missing.
I put the image files into my project folder and basically just add this kinda code in.
Java Syntax (Toggle Plain Text)
Icon smile = new ImageIcon("smile.gif"); label2 = new JLabel("Label with text and smiley icon", smile, SwingConstants.LEFT); label2.setToolTipText("This is label2"); container.add(label2);
So..I'm wondering if anyone can help me with this....
Thanks in advance.
If you set the paths to the image files, such as:
-put the image file here!
Then your jar file should pick them up from wherever you execute it from.
Java Syntax (Toggle Plain Text)
c:/myfolder/smile.gif
Then your jar file should pick them up from wherever you execute it from.
*Voted best profile in the world*
•
•
•
•
Originally Posted by iamthwee
If you set the paths to the image files, such as:
-put the image file here!Java Syntax (Toggle Plain Text)
c:/myfolder/smile.gif
Then your jar file should pick them up from wherever you execute it from.
By the way, are there any ways to like include the image file into the JAR file and read it from there, so that the end product is just one file. Easier for distribution I guess and it eliminates the need to change the codes everytime.
A newbie here, but I think I saw some other files that are in the JAR file, like some kinda resources that are included in it. So, I'm wondering if the images can be included in as well.
Kinda hard when you're switching from something as easy as Delphi where you concentrate more on the programming aspect rather than the GUI....hehe :o
>By the way, are there any ways to like include the image file into the JAR file and read it from there, so that the end product is just one file. Easier for distribution I guess and it eliminates the need to change the codes everytime.
I don't think that's possible, although I'm just guessing here. The only other way out of it, is to ensure your jar file is in the same directory as your pictures. But like you say that has obvious problems. You can't execute it properly if you drag the jar file elsewhere - the pictures won't link it up.
Hmmm.
I don't think that's possible, although I'm just guessing here. The only other way out of it, is to ensure your jar file is in the same directory as your pictures. But like you say that has obvious problems. You can't execute it properly if you drag the jar file elsewhere - the pictures won't link it up.
Hmmm.
*Voted best profile in the world*
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
sabithpocker, so that's how you'd access an image? I agree that the image can be included in the generated jar file. However, my feeling is that the file will not be accessible via the Java code since the current path (using relative addressing) would be the path the jar executable is being run from and not point to the files within the jar file. Consider:
This code was taken from: http://www.devx.com/tips/Tip/5697 and is generally then way one would include an image loaded from an executable jar file. I believe this is what the poster was looking for? Oh, of course adding the image to your project is as simple as dragging the image and putting it into perhaps the source folder of your project (which is on the Projects tab in the NetBeans IDE). This, as the poster said, makes distribution of the project, much more robust.
A very late reply, but something someone else might find useful.
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.net.*; public class ResourceTest extends Frame { Image img; public ResourceTest() throws Exception { URL myurl = this.getClass().getResource("/myimage.gif"); Toolkit tk = this.getToolkit(); img = tk.getImage(myurl); } // public ResourceTest() public void paint(Graphics g) { g.drawImage(img, 0, 0, this); } // public void paint() } // public class ResourceTest extends Frame
This code was taken from: http://www.devx.com/tips/Tip/5697 and is generally then way one would include an image loaded from an executable jar file. I believe this is what the poster was looking for? Oh, of course adding the image to your project is as simple as dragging the image and putting it into perhaps the source folder of your project (which is on the Projects tab in the NetBeans IDE). This, as the poster said, makes distribution of the project, much more robust.
A very late reply, but something someone else might find useful.
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
sabithpocker, so that's how you'd access an image? I agree that the image can be included in the generated jar file. However, my feeling is that the file will not be accessible via the Java code since the current path (using relative addressing) would be the path the jar executable is being run from and not point to the files within the jar file. Consider:
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.net.*; public class ResourceTest extends Frame { Image img; public ResourceTest() throws Exception { URL myurl = this.getClass().getResource("/myimage.gif"); Toolkit tk = this.getToolkit(); img = tk.getImage(myurl); } // public ResourceTest() public void paint(Graphics g) { g.drawImage(img, 0, 0, this); } // public void paint() } // public class ResourceTest extends Frame
This code was taken from: http://www.devx.com/tips/Tip/5697 and is generally then way one would include an image loaded from an executable jar file. I believe this is what the poster was looking for? Oh, of course adding the image to your project is as simple as dragging the image and putting it into perhaps the source folder of your project (which is on the Projects tab in the NetBeans IDE). This, as the poster said, makes distribution of the project, much more robust.
A very late reply, but something someone else might find useful.
As i understood from his reply he only need to address to a location inside jar file
new URL( "jar:file:/" + i_jarFileName + "/!" + "/" + "icon.gif" )
or a similar path can refer to files inside jar file....once you have compiled the image to jar file use such path and if there is any more problem please reply i might refer to my old codes again

(my goal at that time was to include every file in the single jar file like what we have in windows portable executive, i dint want any image to be outside jar)
yes, using getResource or better yet getResourceAsStream is the way to extract resources from a jarfile.
Make sure that those resources are contained in the jarfile as part of the classpath or you'll get an error indicating that the resource doesn't exist.
That means that during compilation (and jarring) the files need to be copied so they exist next to the classfiles for the application.
Make sure that those resources are contained in the jarfile as part of the classpath or you'll get an error indicating that the resource doesn't exist.
That means that during compilation (and jarring) the files need to be copied so they exist next to the classfiles for the application.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Whats wrong with this code?
- Next Thread: Include bean into IDE Project
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






