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 ,  | 71 comments | no trackbacks


  1. Avatar Lucio Benfante said about 13 hours later:

    ...or briefly:

    InputStream is = 
  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 8 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

  7. Avatar Lucio Benfante said 9 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:

  8. Avatar Rui Pereira said 611 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 : 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( at sun.awt.image.URLImageSource.getDecoder( at sun.awt.image.InputStreamImageSource.doFetch( at sun.awt.image.ImageFetcher.fetchloop( at

    any thoughs ? Thx Rp

  9. Avatar Lucio Benfante said 616 days later:

    I tried the following code, and it works:

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

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


  10. Avatar said 880 days later:

    Thanks for the help. Had been looking for this.

  11. Avatar yosef said 1256 days later:

    for key store

    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 1788 days later:

    Look here!

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

    URl url = ClassLoader.getSystemResource(String sourceTarget);


  17. Avatar H Miracle Review said 1912 days later:

    Many thanks on your excellent website. I was pondering no matter if I can achieve some adverts from you? In case you consent to my suggestions, make sure you you should not hesitate to communicate with me by way of email. I’m glad that for those who also have your own personal best good quality suitable blogs.

  18. Avatar marketing mix said 1947 days later:

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

  19. Avatar Sabbir said 1948 days later:

    thanx for the blog. saved my time :)

  20. Avatar RD Web Design said 1959 days later:

    Nice one, cheers for this, been searching for a solution, ta.

  21. Avatar blackberry Spy said 1990 days later:

    Jar file good I never Knew That Thanks for sharing

  22. Avatar Tax Return said 2014 days later:

    This is a wonderful approach on this topic.

  23. Avatar Play Sand said 2022 days later:

    It’s a real pleasure to read everything you write.

  24. Avatar John said 2042 days later:

    amazing phone SEO India

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

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

  26. Avatar SEO India said 2157 days later:

    I must appreciate you for the information you have shared.I find this information very useful and it has considerably saved my time.

  27. Avatar Dentists in Mumbai said 2193 days later:

    I like this post and found very useful for reference. I bookmark this website and refer it to my colleague and friends.

  28. Avatar snyxius said 2199 days later:

    great accessing ya…...

    Web Design Company.
  29. Avatar Blepharoplasty said 2205 days later:

    I like that you have so many fans. I guess you won’t share your secret!

  30. Avatar Liposuction said 2207 days later:

    I find your article excellent.

  31. Avatar Web development india said 2242 days later:

    Thanks for sharing an awesome blog!!!!!!!!

  32. Avatar <b><a href="">quotes about life</a></b> said 2251 days later:

    quotes about life

  33. Avatar quotes about life said 2251 days later:

    quotes about life

  34. Avatar Silver Coast Villa said 2298 days later:

    I found this website very interesting and the post content the good updated information thanks a lot.

  35. Avatar said 2315 days later:

    Thank you for this great information, you write very well which I like very much. I really impressed by your post. Casino Wheels

  36. Avatar Ashiq Ahmad said 2332 days later:

    I have read this post. Collection of post is a nice one ..that am doing Web Development India and ERP Software Solution That I will inform about your post to my friends and all the best for your future posts..

  37. Avatar Paige said 2438 days later:

    A truly a great one you’ve shared in here. thanks for sharing. Impressive indeed.

  38. Avatar Francisco said 2453 days later:

    Thanks you very much!! this Save my time !!

  39. Avatar Denis said 2488 days later:

    Thanks for this.

  40. Avatar said 2502 days later:

    I want to access a class of jar file not having main method in it from the command prompt. I had spent lots of time to google it but unfortunately I didn’t get anything. any help greatly appriciated

    —Thanks Krishna

  41. Avatar alberts john said 2506 days later:

    i have read firSt time ur blog, ur blog is great man,,,,,......

  42. Avatar Lesen Sie hier mehr said 2622 days later:

    This was such a well structured tutorial, very good insight into the application for getting these amazing results.

  43. Avatar Rahul said 2699 days later:

    Wow, Nice post, Thanks for sharing.

  44. Avatar Mad Hatter said 2727 days later:

    This is no trivial to get to work. I think you should extend the sample code to be at least as in comment #9.

    I used that code with the “long form” (maybe not necessary for it to work) and without any path to the image, because after looking into the generated jar, the file was in the root location, not under its physically containing folder. This was the reason it was failing for me and imgURL did not have any value.

  45. Avatar Bangalore said 2762 days later:

    I do accept as true with all the ideas you have presented for your post. They are very convincing and will definitely work. Still, the posts are too brief for newbies. May just you please extend them a bit from next time? Thank you for the post.

  46. Avatar Torrents said 2773 days later:

    I want to access a class of jar file not having main method in it from the command prompt. I had spent lots of time to google it but unfortunately I didn’t get anything. any help greatly appriciated

  47. Avatar Web Designing Company said 2816 days later:

    Some times it was very easy i think so…

  48. Avatar yellow throw pillows said 2876 days later:

    Very informative article so many interesting things i found here which increase my knowledge so much. yellow throw pillows

  49. Avatar kiran said 2896 days later:

    I also much like the design and style of your blog concept. It is actually very very clear and simple to operate Thank you for any other wonderful article.

  50. Avatar <a href="">hivizag</a> said 2941 days later:


  51. Avatar said 2941 days later:

    Nice information in this blog. hivizag

  52. Avatar hivizag said 2941 days later:


  53. Avatar hivizag said 2941 days later:

    Your message

  54. Avatar yuh said 2968 days later:

    Along with acquiring merely potentially by far the most prepared together with knowledgeable dissertation builders with regards to the same as scientific tests we remedy that numerous files currently aren’t going to be more prone to you will be 100% extraordinary, associated with without motive that may are in possession of almost any plagiarism, the concept is commonly fantastic extraordinary ideal for another individual in your case. turrets

  55. Avatar <a href="">vizag beach photos</a> said 2973 days later:

    vizag beach photos

  56. Avatar ddfd said 2978 days later:

    Maybe all the time turn into components made for uncovering exceptional data files all over. right now a number of us should be intrigued on account of your overall survey joined with whatever you make a decision in addition to decide on in addition to choose together with might possibly likely adequately enjoy plenty of could be uptodate joined with definitely noticing, i will aim for determine the complete comprehend your site it is i will provide which might just as before to find out you need to simply just at the moment examine all people, should you have designed inked an excellent execute.

  57. Avatar live chat operators said 3005 days later:

    Great post and exactly matched that’s what i was searching for a long while ago. Thanks for posting.

  58. Avatar technomeet said 3013 days later:

    Thanks for taking the effort to provide us with the tips from the Post.

    Regads, Website Developer

  59. Avatar Dian Prince said 3019 days later:

    I should prefer this site to all. This site is totally good for all the readers. Best way to lack boredom with. leatherp

  60. Avatar mary said 3057 days later:

    Good Post. This is what i was searching for. Complete flawless article grabbing all aspects of the topic. Thanks for posting. Assassins Creed 3 Jacket White and Blue

  61. Avatar Domestic Packages said 3099 days later:

    Thanks for providing such useful information. I think got the answer of my query.

  62. Avatar Friv said 3132 days later:

    Good. Thank you for sharing, this information is useful to me. Play Friv or Friv 10 Game.

  63. Avatar as said 3144 days later:


  64. Avatar prabhas said 3239 days later:

    I was surfing net and fortunately came across this site and found very interesting stuff here

  65. Avatar Astrologer in India said 3241 days later:

    I believe this can be an informative post and it is actually really helpful and knowledgeable. thus, I’d prefer to thank you for the efforts you have made in writing this article.

  66. Avatar dsdsd said 3242 days later:


  67. Avatar prabhas said 3248 days later:

    I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again.

  68. Avatar asasa said 3250 days later:


  69. Avatar Gaurav Ghosh said 3349 days later:

    Nice Website i found on the web with the informative post Web Development

  70. Avatar harga bola soccer said 3357 days later:


  71. Avatar <a href="">essaywriter</a> said 3376 days later:

    I totally agree with what you have said here on this blog. Thanks for sharing the insightful post.


Use the following link to trackback from your own site:

(leave url/email »)

   Comment Markup Help Preview comment