12 comments 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 java.io.FileNotFoundException.
    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
    /myApp.jar
    /lib/lots-of-jar’s
    /config/config.xml
    /resources/my_cat.jpg
    /resources/my_dog.jpg

    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. thanks for this article. was very useful to me, since there is not a lot support on sun’s forums on this topic

  4. 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.
    Thanks

  5. 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

  6. Pingback: How to embed resource files in your jars using Netbeans « :: Optical Hacking :: | P3r3s' Notes

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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