0

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?

user1538162
  • 71
  • 1
  • 1
  • 4

3 Answers3

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

-1

Simple method : you can set your image folder to classpath and export jar file by eclipse.

Jason
  • 1,241
  • 8
  • 16
  • 2
    Question 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
  • @mthmulders so Netbeans have not export jar function ? – Jason Aug 02 '12 at 09:25
  • 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