Adding images to JAR file (Netbean 5.0)

Reply

Join Date: Sep 2005
Posts: 42
Reputation: j1979c is an unknown quantity at this point 
Solved Threads: 0
j1979c's Avatar
j1979c j1979c is offline Offline
Light Poster

Adding images to JAR file (Netbean 5.0)

 
0
  #1
Apr 1st, 2006
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.

  1. Icon smile = new ImageIcon("smile.gif");
  2. label2 = new JLabel("Label with text and smiley icon", smile,
  3. SwingConstants.LEFT);
  4. label2.setToolTipText("This is label2");
  5. container.add(label2);

So..I'm wondering if anyone can help me with this....

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #2
Apr 1st, 2006
If you set the paths to the image files, such as:

  1. c:/myfolder/smile.gif
-put the image file here!

Then your jar file should pick them up from wherever you execute it from.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 42
Reputation: j1979c is an unknown quantity at this point 
Solved Threads: 0
j1979c's Avatar
j1979c j1979c is offline Offline
Light Poster

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #3
Apr 1st, 2006
Originally Posted by iamthwee
If you set the paths to the image files, such as:

  1. c:/myfolder/smile.gif
-put the image file here!

Then your jar file should pick them up from wherever you execute it from.
I see... hmm... I'll check it out.

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #4
Apr 1st, 2006
>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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: sabithpocker has a little shameless behaviour in the past 
Solved Threads: 0
sabithpocker sabithpocker is offline Offline
Newbie Poster

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #5
Mar 18th, 2008
you just need to place the image/image_folder in %project_file%\build\classes.
now when you build the project it will be automativally added to the jar archive, which is now executable jar archive.

i use netbeans 6.0
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #6
Aug 5th, 2008
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:
  1. import java.awt.*;
  2. import java.net.*;
  3. public class ResourceTest extends Frame {
  4.  
  5. Image img;
  6.  
  7. public ResourceTest() throws Exception {
  8. URL myurl = this.getClass().getResource("/myimage.gif");
  9. Toolkit tk = this.getToolkit();
  10. img = tk.getImage(myurl);
  11. } // public ResourceTest()
  12.  
  13. public void paint(Graphics g) {
  14. g.drawImage(img, 0, 0, this);
  15. } // public void paint()
  16.  
  17. } // 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: sabithpocker has a little shameless behaviour in the past 
Solved Threads: 0
sabithpocker sabithpocker is offline Offline
Newbie Poster

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #7
Aug 16th, 2008
Originally Posted by PoovenM View Post
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:
  1. import java.awt.*;
  2. import java.net.*;
  3. public class ResourceTest extends Frame {
  4.  
  5. Image img;
  6.  
  7. public ResourceTest() throws Exception {
  8. URL myurl = this.getClass().getResource("/myimage.gif");
  9. Toolkit tk = this.getToolkit();
  10. img = tk.getImage(myurl);
  11. } // public ResourceTest()
  12.  
  13. public void paint(Graphics g) {
  14. g.drawImage(img, 0, 0, this);
  15. } // public void paint()
  16.  
  17. } // 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.
i did java code long before that i dont remmeber exactly what all i did and i am not sure of what PoovenM is tryin to convey.
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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Adding images to JAR file (Netbean 5.0)

 
0
  #8
Aug 16th, 2008
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC