Adding resources to .jar

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

Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Adding resources to .jar

 
0
  #1
May 2nd, 2009
Hello!

I'm searching and searching and can't find a solution...

In my project I've got a folder 'Images' where from my application loads icons etc. Running project from IDE (NetBeans) it's of course working fine, but I want to create stand-alone application, so I create .jar file.

And here's a problem - how to add that folder 'Images' to my .jar file, so application could load icons from it?

I tried to simply put that folder into .jar file, but it doesn't seem to be that easy...


Zibo
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Adding resources to .jar

 
1
  #2
May 2nd, 2009
This should help. It was the first search result in Google.
Last edited by stephen84s; May 2nd, 2009 at 10:14 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Adding resources to .jar

 
0
  #3
May 2nd, 2009
Certainly, it helps alot. Thank You.

BTW: Funny site. (so offensive! )
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Adding resources to .jar

 
0
  #4
May 2nd, 2009
Ok, it didn't solve my problem as I thought. I do know now how to create, update etc. jar files, but still my application doesn't loads icons from inside of .jar file.

I've got in code such instructions as:

  1. new JLabel( new ImageIcon("Images\\icon.png") );

When I create .jar file the inside looks like:

  1. Images (my icons)
  2. MyPackage (with files .class)
  3. META-INZ (manifest)

And icons are loaded ONLY if Images folder is in same place where .jar file. I mean:

  1. MyFolder:
  2. ..
  3. Images\
  4. myApp.jar


So.. what am I doing wrong then?
Last edited by Zibo; May 2nd, 2009 at 12:46 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Adding resources to .jar

 
0
  #5
May 2nd, 2009
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Adding resources to .jar

 
0
  #6
May 2nd, 2009
You could also simply go to the directory where your netbeans projects are, and drop any files that your program needs in the right place. Then netbeans will automatically include them in your jar file and your program will find them correctly.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Adding resources to .jar

 
0
  #7
May 2nd, 2009
Originally Posted by Ezzaral View Post
Use getClass().getResource() to load them.
So I've changed all parts in my code where I load images to:

  1. new ImageIcon(getClass().getResource("Images\\icon.png")
etc.

I've put folder Images to my package directory. Running project from IDE works fine. So I compiled code, .jar has been created.
Folder Images is located inside .jar . I double click the .jar -> nothing happens. When I run .jar from console I get:

NullPointerException at javax.swing.ImageIcon.(init)(ImageIcon.java) blabla

Originally Posted by BestJewSinceJC View Post
You could also simply go to the directory where your netbeans projects are, and drop any files that your program needs in the right place. Then netbeans will automatically include them in your jar file and your program will find them correctly.
When I put Image directory into my package, so NetBeans could add it to .jar, then created .jar file reads images from outside of .jar, not from inside.


Officially I'm sick of it. I spent 6h on that today and still didn't get it working. x(
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Adding resources to .jar

 
0
  #8
May 2nd, 2009
No, I'm saying you should literally go to the folders on your computer, and change the location of the folder with your images in it from there. I'd give you more specific directions, but I'm no expert with this, I just know it works because I did it less than a week ago. After you do this though, you must click run-> clean & build main project.
Last edited by BestJewSinceJC; May 2nd, 2009 at 10:59 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Adding resources to .jar

 
0
  #9
May 3rd, 2009
I really want to get to know how is this working, so I'll show You how I try to do this the best I can, so You could point precisely where's the mistake.

This is how my NetBeans project looks like. Previously in this directory was Images directory and running project from IDE loaded it from here.

http://img7.imageshack.us/img7/3991/12369439.jpg

Src directory (also have tried to put Images into CircleCross package)

http://img7.imageshack.us/img7/3558/53527242.jpg

Example code loading image. (without using getResource() methods):

  1. setIconImage( new ImageIcon("Images\\drawSmall.png").getImage() );

And the content of generated .jar :

http://img7.imageshack.us/img7/8868/41867443.jpg


None of images are loaded...
Last edited by Zibo; May 3rd, 2009 at 9:02 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Adding resources to .jar

 
0
  #10
May 3rd, 2009
My project for netbeans shows the following packages under src folder:

calendar
application
database
org

Where org contains the image files that calendar uses. So I don't know what problem you are having exactly. I'm no expert. But I do know netbeans 6.5.1 made the jar file that I wanted it to make when all the packages were under src folder. I didn't write the code for the calendar, so I'm not sure how it loads the image, but it probably uses the method Ezzaral suggested, which is what you should be using also.
Last edited by BestJewSinceJC; May 3rd, 2009 at 1:34 pm.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1136 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC