<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>www.jugpadova.it: Accessing a resource within a .jar</title>
    <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Java User Group [Padova]</description>
    <item>
      <title>Accessing a resource within a .jar</title>
      <description>&lt;p&gt;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&amp;#8217;s very simple, here&amp;#8217;s an example to retrieve an image:&lt;/p&gt;

&lt;p&gt;&lt;code lang="java"&gt;
ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));&lt;br/&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In general, you can retrieve an InputStream in the following way:&lt;/p&gt;

&lt;p&gt;&lt;code lang="java"&gt;
InputStream is = this.getClass().getClassLoader()
&amp;nbsp;&amp;nbsp;&amp;nbsp;.getResourceAsStream("yourpackage/mypackage/myfile.xml");
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will run inside or outside the jar. Enjoy !&lt;/p&gt;
</description>
      <pubDate>Sun, 05 Feb 2006 19:08:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:8ad0326e-b2dd-4cd3-ab45-133901572a60</guid>
      <author>Emanuele Gesuato</author>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar</link>
      <category>Tips &amp; Tricks</category>
      <category>jar</category>
      <category>resource</category>
      <trackback:ping>http://www.jugpadova.it/articles/trackback/49</trackback:ping>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Verner</title>
      <description>&lt;p&gt;Look here!&lt;/p&gt;


	&lt;p&gt;ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(&amp;#8220;/.png&amp;#8221;));&lt;/p&gt;


	&lt;p&gt;URl url = ClassLoader.getSystemResource(String sourceTarget);&lt;/p&gt;


	&lt;p&gt;MyWebSite &lt;a href="http://www.vanillaworld.ru" rel="nofollow"&gt;www.vanillaworld.ru&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 30 Dec 2010 16:14:17 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:fc0066c0-cee8-4c11-a597-b39fc26f10eb</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-54400</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by mirli</title>
      <description>&lt;p&gt;bravissimo, worked as advertized!&lt;/p&gt;</description>
      <pubDate>Tue, 09 Nov 2010 07:00:19 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:5fb5b8e7-342b-4094-8c9b-95c2dcf742fe</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-54335</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by web designing india</title>
      <description>&lt;p&gt;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&amp;#8217;ll call it the &amp;#8220;Jar tool&amp;#8221;.&lt;/p&gt;</description>
      <pubDate>Mon, 17 May 2010 02:23:23 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ec6c95dd-1fc4-4f0e-8387-028dbfd6aaeb</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-54079</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Web Design London</title>
      <description>&lt;p&gt;Hi Emanuele&amp;#8230;!!!!!!&lt;/p&gt;


	&lt;p&gt;I think the given idea by you is very interesting&amp;#8230;
This all sounds super cool because this is the thing for which I was been looking&amp;#8230;
Thanks for me such an useful information..
great stuff..!!!
Keep the good work..
Hope to see your more and more post so on.&lt;/p&gt;</description>
      <pubDate>Wed, 21 Apr 2010 23:55:50 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:072572e5-d9d8-46f9-ac5a-2fba1438b1ac</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-54066</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Web Design London</title>
      <description>&lt;p&gt;Thanks for providing such useful information. I really appreciate your professional approach.&lt;/p&gt;</description>
      <pubDate>Tue, 23 Mar 2010 05:53:32 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:01fc13d2-938b-46e4-b731-1414994c289b</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-54061</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by yosef</title>
      <description>&lt;p&gt;for key store&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.delphifaq.com/faq/f5003.shtml" rel="nofollow"&gt;http://www.delphifaq.com/faq/f5003.shtml&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;simple and works&lt;/p&gt;</description>
      <pubDate>Thu, 16 Jul 2009 15:12:22 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3e86fc1c-1681-42f5-b0dc-682b0c41a6db</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-54021</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by pant.uday@gmail.com</title>
      <description>&lt;p&gt;Thanks for the help. Had been looking for this.&lt;/p&gt;</description>
      <pubDate>Fri, 04 Jul 2008 23:55:15 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a92dfe3f-45f0-404e-ba52-9ba6d460a3f4</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-53986</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Lucio Benfante</title>
      <description>&lt;p&gt;I tried the following code, and it works:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;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(&amp;quot;images/image.gif&amp;quot;);
        Image image = Toolkit.getDefaultToolkit().getImage(imgURL);
        System.out.println(image);
    }
}&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Check that imgURL is not null. I think it could be a classloading problem. Try also with the &amp;#8220;long&amp;#8221; form:&lt;/p&gt;


	&lt;p&gt;getClass().getClassLoader().getResource(...&lt;/p&gt;</description>
      <pubDate>Mon, 15 Oct 2007 08:33:11 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c7f74a46-9e11-4de7-a13b-796b6de08524</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-53926</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Rui Pereira</title>
      <description>&lt;p&gt;Hi there. 
This all sounds super cool, but I can&amp;#8217;t get it work.&lt;/p&gt;


	&lt;p&gt;I got a JAR file with a default class with a main method to be called.&lt;/p&gt;


	&lt;p&gt;so the inside of the JAR looks like so :&lt;/p&gt;


	&lt;p&gt;com/me/stuff/Bling.class
images/image.gif
META-INF/MANIFEST.MF&lt;/p&gt;


	&lt;p&gt;All works except for the image bit !&lt;/p&gt;


	&lt;p&gt;in my Bling class I got :&lt;/p&gt;


	&lt;p&gt;java.net.URL imgURL = getClass().getResource(&amp;#8220;images/image.gif&amp;#8221;);
image = Toolkit.getDefaultToolkit().getImage(imgURL);&lt;/p&gt;


	&lt;p&gt;the exception I get when I run my  dudle.jar file (  java -jar  dudle.jar ) is :&lt;/p&gt;


	&lt;p&gt;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)&lt;/p&gt;


	&lt;p&gt;any thoughs ?
Thx
Rp&lt;/p&gt;</description>
      <pubDate>Wed, 10 Oct 2007 19:21:39 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c4f4b35b-b222-464e-9328-df1df9f50fa7</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-53925</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Lucio Benfante</title>
      <description>&lt;p&gt;&amp;#8220;my jug&amp;#8221;,
it&amp;#8217;s not my sentence: Emanuele wrote the original post.&lt;/p&gt;


	&lt;p&gt;What is not clear or unusual in distributing a Java application in a Jar or through the Java Web Start?&lt;/p&gt;


	&lt;p&gt;Some links:&lt;/p&gt;


	&lt;p&gt;&lt;a &gt;http://java.sun.com/products/javawebstart/&lt;/a rel="nofollow"&gt;&lt;br /&gt;
&lt;a &gt;http://mindprod.com/jgloss/javawebstart.html&lt;/a rel="nofollow"&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 15 Feb 2006 07:10:05 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:0d6d2992-86fb-4b6b-a7da-0f7ac51ca004</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-61</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by my jug</title>
      <description>&lt;p&gt;Sorry fante,&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m not a trolling , simply I don&amp;#8217;t understand your sentence &amp;#8220;Sometimes it is useful to distribute an application in a jar file through Java Web Start or any other way&amp;#8221; and the explanation with your code.&lt;/p&gt;


	&lt;p&gt;.. but it&amp;#8217;s not important , it&amp;#8217;s only curiosity &amp;#8230;&lt;/p&gt;


	&lt;p&gt;Yes, of sure, the next time I don&amp;#8217;t back in jugpadova.it&lt;/p&gt;</description>
      <pubDate>Tue, 14 Feb 2006 17:59:27 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:ce3f46d3-5721-4e6c-bf6e-616591c51c09</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-60</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Lucio Benfante</title>
      <description>&lt;p&gt;No, with that code you can read a &amp;#8220;file&amp;#8221; from everywhere, without worrying about the real path or the storage of the &amp;#8220;file&amp;#8221; (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.&lt;/p&gt;


	&lt;p&gt;&amp;#8220;my jug&amp;#8221;, about the other questions&amp;#8230;are them on topic? I think you are just trolling. If you are really interested, we have also a mailing-list&amp;#8230;but&amp;#8230;no stupid questions, please.&lt;/p&gt;</description>
      <pubDate>Tue, 14 Feb 2006 07:00:49 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:48287a5e-c071-41e7-a574-814df81d85d8</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-59</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by my jug</title>
      <description>&lt;p&gt;boh , I can&amp;#8217;t understand , however It&amp;#8217;s not so important!&lt;/p&gt;


	&lt;p&gt;Mah , maybe simply ,with that code you can read a file into jar file.&lt;/p&gt;


	&lt;p&gt;but &amp;#8230;.&lt;/p&gt;


	&lt;p&gt;What&amp;#8217;s means distribuite an application ?? Where is RMI ?? What&amp;#8217;s Java Web Start ??&lt;/p&gt;</description>
      <pubDate>Sat, 11 Feb 2006 22:41:19 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:def788aa-cb56-46c7-8394-91570a6c7053</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-55</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Dario Santamaria</title>
      <description>&lt;p&gt;&lt;i&gt;The &amp;#8221;.jar&amp;#8221; into code or in your explanation&lt;/i&gt; must be in your classpath. So no &lt;i&gt;name of the &amp;#8221;.jar&amp;#8221;&lt;/i&gt; is required inside the code. You can include your jar in your classpath using:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;java -cp .:&amp;lt;myjarfile&amp;gt;.jar myapplication&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Mon, 06 Feb 2006 17:36:56 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:0766190c-439b-4ac4-a3fd-020851f34213</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-52</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by my jug</title>
      <description>&lt;p&gt;I think , it&amp;#8217;s not complete demonstrative.
Where is the code, and the &amp;#8221;.jar&amp;#8221; into code or in your explanation??&lt;/p&gt;</description>
      <pubDate>Mon, 06 Feb 2006 11:44:04 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:32c9d5b7-3d85-4653-8a16-690e3326fd04</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-51</link>
    </item>
    <item>
      <title>"Accessing a resource within a .jar" by Lucio Benfante</title>
      <description>&lt;p&gt;...or briefly:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;InputStream is = 
    this.getClass()
    .getResourceAsStream(
        &amp;quot;yourpackage/mypackage/myfile.xml&amp;quot;);&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Mon, 06 Feb 2006 07:56:25 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:c287f9eb-0e12-483b-93bd-2af086c6228d</guid>
      <link>http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar#comment-50</link>
    </item>
  </channel>
</rss>

