Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Images in jar  RSS feed

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9493
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to provide us with more details. How can you tell that the jar file "does not take them"?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 23867
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A drag and drop of the folder to your project tree should work.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me the files cant be found, is the path correct inside your project tree?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the path is correct. When I open the project in netbeans they appear. I have problems just with the jar
 
Eduardo Lomonaco
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try with this example for a directory tree like:




Try using absolute path too...
 
Paul Clapham
Sheriff
Posts: 23867
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Sheriff
Posts: 23867
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me? Okay, I guess. Can I have some context?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 23867
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 23867
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you care to share your solution, to help the hundreds of people who have similar problems every week?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!