| Author |
Images in jar
|
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
|
I have a problem with the jar file. I have some images, i put them in a separate folder in the project, added that folder as a source, but the jar file still does not take them. Does anybody have an idea why? Or what I should do in order to put them in the jar file? I use Netbeans.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
|
You need to provide us with more details. How can you tell that the jar file "does not take them"?
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
|
Now I see that actually it does not let me add the folder to the resources. Basically, I have in my project a folder named "images", where I put all the images that appear in the program. If I make the jar file like this, when I start the application with the jar file, It does not show me any of the images that were supposed to be there. What do I have to do in order tu insert the images into the jar file?
|
 |
Eduardo Lomonaco
Greenhorn
Joined: Oct 13, 2011
Posts: 13
|
|
Using the project build from netbeans should already place them in the jar file.
A jar file can be opened using tools like jar, 7zip or WinRar.
Did you check with one of those tools if your jar really doesnt have the picture inside it?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
You still haven't established that the images aren't in the jar file. All you have established is that your program doesn't find them, which isn't necessarily a reliable test. Perhaps your program isn't looking in the right place, for example.
|
 |
Eduardo Lomonaco
Greenhorn
Joined: Oct 13, 2011
Posts: 13
|
|
|
A drag and drop of the folder to your project tree should work.
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
I did that now, the images are inside. Then why can't I see them in my program?
Icon addi = new ImageIcon("images/add.gif");
add.setIcon(addi);
This is what I wrote in my program (add is a button).
|
 |
Eduardo Lomonaco
Greenhorn
Joined: Oct 13, 2011
Posts: 13
|
|
|
It seems to me the files cant be found, is the path correct inside your project tree?
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
|
Yes, the path is correct. When I open the project in netbeans they appear. I have problems just with the jar
|
 |
Eduardo Lomonaco
Greenhorn
Joined: Oct 13, 2011
Posts: 13
|
|
Try with this example for a directory tree like:
Try using absolute path too...
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Rus Corina wrote:
If you read the documentation for that constructor, you'll see that the parameter is treated as a file name. Things inside your jar aren't files and don't have file names. They are resources and you can access them via the Class.getResource() method, which returns a URL. Also in the ImageIcon you'll see there's a constructor which takes a URL, so you're all set:
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
@Paul Clapham: Now I have an error
Unable to delete file C:\Documents and Settings\Cori\My Documents\NetBeansProjects\ECLIENT\dist\ECLIENT.jar
(My application is called ECLIENT)
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Me? Okay, I guess. Can I have some context?
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
Nevermind that error, problems with computer. But i have a new one that generated from putting what you suggested in my code. The code looks like this:
private Icon addi = new ImageIcon(this.getClass().getResource("/images/add.gif"));
add.setIcon(addi);
and the error is: Exception in thread "main" java.lang.NullPointerException
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
So getResource() returns null because it can't find that resource in your classpath. That means that your jar doesn't contain an "images" folder at its root, or if it does, that the "images" folder doesn't contain an entry named "add.gif".
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
|
thank you very much for your help. I found something on the internet dealing with url and it worked. Thank you again for your help
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Would you care to share your solution, to help the hundreds of people who have similar problems every week?
|
 |
Rus Corina
Ranch Hand
Joined: Jul 08, 2011
Posts: 90
|
|
URL url = Clienti.class.getResource("/add.gif");
private Icon addi = new ImageIcon(url);
add.setIcon(addi);
This is the solution. add is a button, Clienti is my main class, and the images are placed in a folder called "images" in the project folder
|
 |
 |
|
|
subject: Images in jar
|
|
|