Results 1 to 5 of 5
- 09-22-2012, 11:23 AM #1
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:
The variable repository is just a string that is "../repository".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; } }
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
- 09-22-2012, 01:16 PM #2
Re: Getting a Resource from within a Jar file
The advice in java - getResource with parent directory reference - Stack Overflow may be useful.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-22-2012, 05:55 PM #3
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.
My API:Java Code:cat > a.out || cat > main.class
- 09-22-2012, 10:20 PM #4
Re: Getting a Resource from within a Jar file
Why do they call it rush hour when nothing moves? - Robin Williams
- 09-24-2012, 10:43 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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
-
How to use resource (.res) file in your java me project
By Basit781 in forum CLDC and MIDPReplies: 3Last Post: 11-24-2010, 04:58 AM -
Resource File
By dvlchd3 in forum EclipseReplies: 2Last Post: 01-13-2010, 10:32 AM -
Jar resource file size
By OrangeDog in forum Java AppletsReplies: 1Last Post: 04-30-2009, 04:12 AM -
Reading a properties file using resource bundle.
By rudrakanth in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:23 PM -
P2P resource sharing
By vishal.babar07@gmail.com in forum Advanced JavaReplies: 0Last Post: 02-12-2009, 02:48 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks