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 !



...or briefly:
I think , it’s not complete demonstrative. Where is the code, and the ”.jar” into code or in your explanation??
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:
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 ??
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.
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
“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
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
I tried the following code, and it works:
Check that imgURL is not null. I think it could be a classloading problem. Try also with the “long” form:
getClass().getClassLoader().getResource(...
Thanks for the help. Had been looking for this.
for key store
http://www.delphifaq.com/faq/f5003.shtml
simple and works
Thanks for providing such useful information. I really appreciate your professional approach.
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.
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”.
bravissimo, worked as advertized!
Look here!
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(“/.png”));
URl url = ClassLoader.getSystemResource(String sourceTarget);
MyWebSite www.vanillaworld.ru
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.
Great literature is simply language charged with meaning to the utmost possible degree
Thanks for providing such useful information. I really appreciate your professional approach
thanx for the blog. saved my time :)
Nice one, cheers for this, been searching for a solution, ta.
Very interesting blog.
Jar file good I never Knew That Thanks for sharing
Would it be good to get on your guard associated with an total job caused by a number of undesirable oranges? You’ll find at the least a pair of critical variances, it seems like in my opinion. 1st, no person concerns that will scientific disciplines actually works, no matter what wrongly diagnosed along with fake assert may possibly every now and then always be presented. Nevertheless no matter whether you’ll find just about any “miraculous” solutions via faith-healing, outside of the human body’s individual capacity to treat themselves, is incredibly very much in matter. Second, your expose’ involving scam along with miscalculation throughout scientific disciplines is done virtually entirely by simply scientific disciplines. Though the direct exposure involving scam along with miscalculation throughout faith-healing is actually certainly not accomplished by simply various other faith-healers.
This is a wonderful approach on this topic.
It’s a real pleasure to read everything you write.
amazing phone SEO India
An excellent written article. You should write more on this.
I was looking for some topics that are enough popular and finally founded your blog, it has superb topics with great popularity.
thanxs for sharing such a nice infornmation .
I must appreciate you for the information you have shared.I find this information very useful and it has considerably saved my time.
nice information useful for everyone.
I like this link.
This site very useful.
I like this post and found very useful for reference. I bookmark this website and refer it to my colleague and friends.
great accessing ya…...
Web Design Company.I like that you have so many fans. I guess you won’t share your secret!
I really think your blog is among the best in your niche.
I find your article excellent.
I am very glad I’ve discovered your blog.
I appreciate you!!!!!!!!! Thanks for discussion!!!!!!!!
quotes about life
quotes about life
I actually wanted to post a simple comment to be able to express gratitude to you for all of the unique strategies you are showing at this website. My rather long internet look up has now been recognized with wonderful facts and techniques to write about with my relatives. I ‘d believe that most of us readers actually are rather fortunate to be in a decent site with very many brilliant people with useful strategies. I feel extremely fortunate to have seen your site and look forward to plenty of more awesome moments reading here. Thanks once again for everything.
Everyday is an interesting new life adventure. Impressive indeed. thanks
Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community.
A truly a great one you’ve shared in here. thanks for sharing. Impressive indeed.
I found it fascinating content and information. I like to read this material. Your point of view set people thinking
That’s not me usually motivated by educational posts about Ny sida, on the other hand short article genuinely made me ponder your points of views. You might have provided important and reliable ideas which are realistic and attention-grabbing. I expertly love to write about email list of cpas. Therefore I am aware of the effort it will need to produce an interesting article this way. Thank you for disclosing your professional work.
That’s not me usually motivated by educational posts about Ny sida, on the other hand short article genuinely made me ponder your points of views. You might have provided important and reliable ideas which are realistic and attention-grabbing. I expertly love to write about email list of cpas. Therefore I am aware of the effort it will need to produce an interesting article this way. Thank you for disclosing your professional work.
I saw this really great post today and I wanted to link to it. Thanks again for taking the time to post.
this is the best website ever!!!
Thanks you very much!! this Save my time !!
I agree with you. This post is truly inspiring. I like your post and everything you share with us is current and very informative, I want to bookmark the page so I can return here from you that you have done a fantastic job
Thanks for this.
I don’t think that it will work. A coding will not give a proper meaning and will not give the definition that you want. Plus many codes means more confusing hierarchy and time constraint.
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
i have read firSt time ur blog, ur blog is great man,,,,,......
Thanxs for sharing such a nice infornmation .
What a great post – I have been using Thesis for about a year now and am just scratching the surface of what I can do with it. I can’t wait to go try these suggestions on my own sites. Thanks so much for sharing!
This was such a well structured tutorial, very good insight into the application for getting these amazing results.
I really gives my special thanks to Emanuele Gesuato.Your ideas really worked for me . The way retrieve an InputStream is awsum. Pretty Informative.