Suppose you want to add some images to your JAR Java application. This has several advantages such as security, portability, conveninece for you and your users (don’t need to copy around several files – everything is nicely packaged and embedded into one JAR file).

How can we embed the resources into the app (the .jar)?

For the sake of this example let’s assume that you want to add several icons and other images to you jar and that all these images are stored somewhere in a directory called images on your hard disk, e.g. in /home/user/images.

1. If you are using Netbeans:
– go to your “src” directory (usually under your project directory), This is the directory where all the .java files of your project are stored.
– copy the images dir to this directory:
   cp -r /home/user/images .

Now when you clean-build the project the images subdirectory will be added to your jar file!

2. If you are not using Netbeans or if you simply want to add the resources manually to the jar file:
   jar -uf MyApp.jar /home/user/images

To list the contents of the JAR:
   jar -tf MyApp.jar

You should see that the images directory is now embedded inside the jar.

Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

Set your Twitter account name in your settings to use the TwitterBar Section.