Accessing a resource within a .jar

Posted by Emanuele Gesuato Mon, 06 Feb 2006 01:08:00 GMT

Sometimes it is useful to distribute an application in a jar file through Java Web Start or any other way. So, you could have to read some resource (images or properties file) from inside a jar. How can you do it ? It’s very simple, here’s an example to retrieve an image:

ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));

In general, you can retrieve an InputStream in the following way:

InputStream is = this.getClass().getClassLoader()    .getResourceAsStream("yourpackage/mypackage/myfile.xml");

It will run inside or outside the jar. Enjoy !

Posted in  | Tags ,  | 25 comments | no trackbacks

Comments

  1. Avatar Lucio Benfante said about 13 hours later:

    ...or briefly:

    InputStream is = 
        this.getClass()
        .getResourceAsStream(
            "yourpackage/mypackage/myfile.xml");
  2. Avatar my jug said about 17 hours later:

    I think , it’s not complete demonstrative. Where is the code, and the ”.jar” into code or in your explanation??

  3. Avatar Dario Santamaria said about 22 hours later:

    The ”.jar” into code or in your explanation must be in your classpath. So no name of the ”.jar” is required inside the code. You can include your jar in your classpath using:

    java -cp .:<myjarfile>.jar myapplication
  4. Avatar my jug said 6 days later:

    boh , I can’t understand , however It’s not so important!

    Mah , maybe simply ,with that code you can read a file into jar file.

    but ….

    What’s means distribuite an application ?? Where is RMI ?? What’s Java Web Start ??

  5. Avatar Lucio Benfante said 8 days later:

    No, with that code you can read a “file” from everywhere, without worrying about the real path or the storage of the “file” (resource), that can even be in a jar or elsewhere in the net, or in a database. It delegates the loading to the classloader, so using the same machanism used for loading the classes in the JVM.

    “my jug”, about the other questions…are them on topic? I think you are just trolling. If you are really interested, we have also a mailing-list…but…no stupid questions, please.

  6. Avatar my jug said 9 days later:

    Sorry fante,

    I’m not a trolling , simply I don’t understand your sentence “Sometimes it is useful to distribute an application in a jar file through Java Web Start or any other way” and the explanation with your code.

    .. but it’s not important , it’s only curiosity …

    Yes, of sure, the next time I don’t back in jugpadova.it

  7. Avatar Lucio Benfante said 10 days later:

    “my jug”, it’s not my sentence: Emanuele wrote the original post.

    What is not clear or unusual in distributing a Java application in a Jar or through the Java Web Start?

    Some links:

    http://java.sun.com/products/javawebstart/
    http://mindprod.com/jgloss/javawebstart.html

  8. Avatar Rui Pereira said 612 days later:

    Hi there. This all sounds super cool, but I can’t get it work.

    I got a JAR file with a default class with a main method to be called.

    so the inside of the JAR looks like so :

    com/me/stuff/Bling.class images/image.gif META-INF/MANIFEST.MF

    All works except for the image bit !

    in my Bling class I got :

    java.net.URL imgURL = getClass().getResource(“images/image.gif”); image = Toolkit.getDefaultToolkit().getImage(imgURL);

    the exception I get when I run my dudle.jar file ( java -jar dudle.jar ) is :

    Uncaught error fetching image: java.lang.NullPointerException at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97) at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:107) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

    any thoughs ? Thx Rp

  9. Avatar Lucio Benfante said 617 days later:

    I tried the following code, and it works:

    package provaimage;
    
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.net.URL;
    
    public class Main {
    
        public static void main(String[] args) {
            URL imgURL = Main.class.getResource("images/image.gif");
            Image image = Toolkit.getDefaultToolkit().getImage(imgURL);
            System.out.println(image);
        }
    }

    Check that imgURL is not null. I think it could be a classloading problem. Try also with the “long” form:

    getClass().getClassLoader().getResource(...

  10. Avatar pant.uday@gmail.com said 880 days later:

    Thanks for the help. Had been looking for this.

  11. Avatar yosef said 1257 days later:

    for key store

    http://www.delphifaq.com/faq/f5003.shtml

    simple and works

  12. Avatar Web Design London said 1506 days later:

    Thanks for providing such useful information. I really appreciate your professional approach.

  13. Avatar Web Design London said 1536 days later:

    Hi Emanuele…!!!!!!

    I think the given idea by you is very interesting… This all sounds super cool because this is the thing for which I was been looking… Thanks for me such an useful information.. great stuff..!!! Keep the good work.. Hope to see your more and more post so on.

  14. Avatar web designing india said 1561 days later:

    To perform basic tasks with JAR files, we can use the Java Archive Tool Because the Java Archive tool is invoked by using the jar command, for convenience we’ll call it the “Jar tool”.

  15. Avatar mirli said 1737 days later:

    bravissimo, worked as advertized!

  16. Avatar Verner said 1789 days later:

    Look here!

    ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(“/.png”));

    URl url = ClassLoader.getSystemResource(String sourceTarget);

    MyWebSite www.vanillaworld.ru

  17. Avatar Sabbir said 1948 days later:

    thanx for the blog. saved my time :)

  18. Avatar http://www.godwinhotels.in said 1969 days later:

    Very interesting blog.

  19. Avatar mcihael said 2015 days later:

    Very Cheap Pro Cycling Online Shop www.bike-jersey.net for Cycling Jerseys.They have great quality and very cheap price ,also could offer the great service and fast shipping!!!

  20. Avatar ANSI Flange said 2041 days later:

    Thank you for sharing. Very happy to see these and believe that your point of view. digital inverter generator is backup equipments for every home, which are used for the purpose of supplying electricity at the time of power failure.

  21. Avatar Find a tradesman on iPhone said 2058 days later:

    An excellent written article. You should write more on this.

  22. Avatar Bulk DVD Replication ideareplication.com said 2059 days later:

    I was looking for some topics that are enough popular and finally founded your blog, it has superb topics with great popularity.

  23. Avatar Web design said 2144 days later:

    nice information useful for everyone Web design

  24. Avatar Mohan lal said 2157 days later:

    thanxs for sharing such a nice infornmation .

  25. Avatar spy blackberry said 2177 days later:

    Thanks for this excellent written information i hope you will write more on this subject i am glad that you are sharing your knowledge with us

Trackbacks

Use the following link to trackback from your own site:
http://www.jugpadova.it/articles/trackback/49

(leave url/email »)

   Comment Markup Help Preview comment