Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By kammce

Thread: Getting a Resource from within a Jar file

  1. #1
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Getting a Resource from within a Jar file

    Getting a Resource from within a Jar file, using a relative path. The file that I am using to get this file is in path/to/file/Control.class. The file I want is path/to/repository/img.png. How do I get this file and use it as an ImageIcon? My attempt is using a method:

    Java Code:
    public ImageIcon getImageIcon(String url) 
    {
    	ImageIcon retzero = new ImageIcon();
    	try 
    	{
    		ImageIcon ret = new ImageIcon(this.getClass().getResource(repository+url));
    		return ret;
    	} catch (java.lang.NullPointerException e) {
    		System.out.println(this.getClass().getName()+" could not find: " + repository+url);
    		return retzero;
    	}
    }
    The variable repository is just a string that is "../repository".

    Also, I tried using, get ResourceAsStream(), but that caused some slowdowns in my program. A lot of images are being used.

    Thank you for Reading.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Getting a Resource from within a Jar file

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Getting a Resource from within a Jar file

    Thank you very much DarryleBurke! You advice helped me a lot. Although, it was not the direct solution, it did point me in the exact direction to the solution.

    The solution is this:

    I need a file named /source/repository/img.png
    There is a Java file within the directory /source/controls/controller.java

    The method that I used to get the image file before was to use a URL string "../repository/img.png". This works if Java can access the outside FileSystem. In my case, I wanted Java to use the files stored within the Jarfile zip filesystem. In which case, "../" does not work, and is probably not a good idea to use after this point, even when it does work.

    Since controller.java was in the package source.controls, it could get the resource file img.png by making the URL string "source/repository/img.png" Just as long as I used Controls.class.getClassLoader().getResource(reposi tory);. As long as the directory is in package range, it can be retrieved in this fashion.

    I personally did not know packages worked this way, so if anyone else did not know this either, hopefully this will help them out.

    EDIT: yeah, this is solved now!
    Last edited by kammce; 09-22-2012 at 05:58 PM.
    DarrylBurke likes this.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Getting a Resource from within a Jar file

    Quote Originally Posted by kammce View Post
    Thank you very much DarryleBurke! You advice helped me a lot. Although, it was not the direct solution, it did point me in the exact direction to the solution.
    You're welcome, and that's what we always try (but don't always succeed) to do here: provide a pointer to resources and leave the pleasure of solving the problem to you!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Getting a Resource from within a Jar file

    I wouldn't get the ClassLoader method, as it's pointless.
    If you just want to start at the root of the jar file stick a "/" at the beginning.
    The ClassLoader resource stuff has different rules to the Class.getResource method so you can quickly confuse matters if you mix them.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. How to use resource (.res) file in your java me project
    By Basit781 in forum CLDC and MIDP
    Replies: 3
    Last Post: 11-24-2010, 04:58 AM
  2. Resource File
    By dvlchd3 in forum Eclipse
    Replies: 2
    Last Post: 01-13-2010, 10:32 AM
  3. Jar resource file size
    By OrangeDog in forum Java Applets
    Replies: 1
    Last Post: 04-30-2009, 04:12 AM
  4. Reading a properties file using resource bundle.
    By rudrakanth in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:23 PM
  5. P2P resource sharing
    By vishal.babar07@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2009, 02:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •