How to embed resource files in your jars using Netbeans

How to embed various file types as resources in your Netbeans jar files

Following the previous article about libraries jar file packaging on the path to “I want all my original processing features in my IDE“, we are going to detail how to replicate the processing functionality of embedding the contents of the /{SketchPath}/data/ folder in the resulting application.

So, we would like to include resources (images, sound, data, shaders, etc) in our applications so that we don’t need to use file paths and can package these resources in the application’s jar file ? And we would also like to access these resources from our code ? Here is how to do it.. Step 1 : Adding resource files to your project

The is done natively by Netbeans using the following steps :

  1. Locate the src folder in your NetBeans project folder. This will have subfolder(s) in it that correspond to the package(s) in your application. The final package folder will have the source code (.java files) for your application.
  2. Create a subfolder for the resources in the folder containing the .java files. Name this folder accordingly (like “resources” or “images” or whatever might be appropriate for your data.
  3. Copy your data files into this folder. Keep a backup copy of the data — if you put it in the wrong folder, NetBeans may delete it.
  4. Clean and Build your project. This will compile the code and embed the contents of the resources folder into the application’s .jar file.
    (Note : These contents will also be copied to the build/classes/your-packages/ folder of the NetBeans project.)

Ok, we are done with the embedding part ! Now, the access part ..

Step 2 : Accessing embedded resource files from your project

Each java Class object contains a set of two overloaded getResourceAsStream(...) methods that can access the embedded resources. So, we have to obtain a Class object reference in order to call these methods. This can be done in the following ways :

  1. If you don’t have an instance of the object you want to get the Class object for, just use :
  2. If you do have an instance of the object just call the usual :

So.. the code to get an InputStream from a resource file named “bumpMap.png” and located in the {SourceFolder}/resources/images/ folder of your application source folder and for a Main class named MySketch (ffew;-), the code would be :

final String resourcesPath = “resources/images/bumpMap.png”;
InputStream stream = MySketch.class.getResourceAsStream(resourcesPath);

And voila ! We can now embed any resource file directly in our jars. I invite the readers to give me their ideas and recommandations about their best ideas and ways to use this feature.

Delicious tags : , ,
About these ads

13 thoughts on “How to embed resource files in your jars using Netbeans”

  1. In the name of Allah…
    Thanks a lot it was very nice article. But I want to use it for reading a text file with FileInputStream class. My text files reside in resources/data/. But I have gotten
    Any suggestions will be appreciated.

  2. Fine, but normally You do not put resources in the classes package tree, but outside.

    Normally You have the following structure

    You have to create Your own build.xm instead netbeans build file. In this in the jar target You have to jar with a ClassPath entry in the manifest attribute that references those outside directories.

  3. This is a very useful post, but this is only for resources which have to be read. What about resources which have to be written? For instance if I want a text file in my program and I want to modify it, how can I find the related OutputStream? Is there a class similar to getResourceAsStream?

    I know that this post is old, but I would be very very happy if you can answer.

  4. Yo this looks great. I tried writing to a txt file in the jar, can you see what’s causing the exception? Thanks.

    public static final String QUESTION_FILE = “res/question.txt”; //A txt file in a res folder

    InputStream s = QuestionMain.class.getClassLoader().getResourceAsStream(QUESTION_FILE); //For reading, so far so good.

    PrintStream ps = new PrintStream(new File(QuestionMain.class.getResource(QUESTION_FILE).toURI())); //Tried this for writing to the txt file in the jar, but at this line the following exception i thrown at runtime only (compiles ok):

    Exception in thread “main” java.lang.IllegalArgumentException: URI is not hierarchical

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s