bcapital lsmall 0small gsmall psmall osmall ssmall tsmall

Posted : 29th January, 2012

How to add images in your Eclipse Project that can work for your Runnable .jar file.

1.)  The first step for the tutorial is to make a New Java Project in Eclipse. For this Click on File -> New -> Java Project. On the New Java Project window, enter any name under Project name field, as shown in Figure 3.1 and click Finish.

Figure 3.1

2.)  Now Expand your Project, under Project Explorer and Right Click your src Folder under your Project (Like in my case it was SwingTest, so I will expand SwingTest, and then Right Click on src Folder) and then go to New -> Package and provide some name to the package, as shown in Figture 3.2 and then click Finish.

Figure 3.2

3.)  Now Right Click your package, under Project Explorer and then go to New -> Class and provide any name to the class inside the Name field, as shown in Figture 3.3 and then click Finish.

Figure 3.3

4.)  Now write the code as described in the following figure i.e. Figure 3.4 or use your own code in it's place, inside the class just created in the previous step. Once done Save the .java file and here is the link to the image mentioned in the code caIcon.png.

Figure 3.4

5.)  Now Right Click your Project from Project Explorer and then go to New -> Source Folder and provide any name to the folder inside the Folder name field, as shown in Figture 3.5 and then click Finish.

Figure 3.5

6.)  Now from your File System i.e. manually go to the just created folder. And Paste the Images inside this folder. Or else, if you wanted to create a new folder, then create a New Folder inside resources folder, give it some name and then paste your images inside this newly created folder.

7.)  Now Refresh your Project by going back to the Eclipse IDE from Project Explorer .

8.)  Now watch the getImage(String path) method as written in the code supplied above in Figure 3.4. Here the value of the path depends on what is inside your resources Folder. Say if you created only resources Folder and added your images to that, then path = "/yourImage.IMAGEEXTENSION". But say if you manually added another folder say images inside resources folder and added your images to this new folder, then path = "/images/yourImage.IMAGEEXTENSION". Note : Do remember to add the first forward slash.

Final Result

Now you can Export your Project to create a Runnable Jar, and your icons can be easily seen, without you have to do anything extra to your code or classpath.
Hoping that this POST must have helped you in some way.