i am developing a swing application using netbeans 7.1.2 . In this, i am using some image files. The image directory is currently in the dist folder. I want to attach the files with the jar file so that i can carry it anywhere as a SINGLE (jar)file. can anyone please help me with a solution?
Asked
Active
Viewed 333 times
0
-
1This was already asked. See the solution here: http://stackoverflow.com/questions/1096398/add-image-to-jar-java – Christian Lendel Aug 02 '12 at 09:17
3 Answers
2
Assuming your project is Maven-based, I would place the images in the src/main/resources
folder. Maven will automatically include the files in that folder into your JAR-file.
Otherwise, this blogpost will help you out.

mthmulders
- 9,483
- 4
- 37
- 54
-
Am sorry, but wat do u mean by "Maven-based"? Also my src/main folder doesnt have any files other than the source code.(.java file). I copied the images folder in src/main folder. But then also NO change. – user1538162 Aug 03 '12 at 13:10
-
If you have a `pom.xml` file in the root folder of your project, it's probably Maven-based. If you have that, copy the images to `src/main/resources` (not `src/main`). Otherwise, follow the blogpost I linked earlier. – mthmulders Aug 03 '12 at 13:13
0
Oi Mate
First Create a package(Say images) under Source Packages
copy all of your images to this package(when you create a package,a folder in the name of your package will be created inside your project src folder, so copy images to it)
You can access your Images from your program as
URL imageurl = getClass().getResource("/images/imagename");//assuming your package name is images
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));
Now use this JLabel piclabel

Joyal Augustine
- 180
- 12
-1
Simple method : you can set your image folder to classpath and export jar file by eclipse.

Jason
- 1,241
- 8
- 16
-
2Question is about Netbeans, so telling how to do this in Eclipse is far from useful; hence a downvote. – mthmulders Aug 02 '12 at 09:21
-
-
2@Jason That's not what he said. He said that the question is in the context of the Netbeans IDE. Providing a solution involving Eclipse IDE is therefore not helpful. Hence your -1 vote from mthmulders. – Radu Murzea Aug 02 '12 at 09:27