jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes accessing resources from a jar file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "accessing resources from a jar file." Watch "accessing resources from a jar file." New topic

accessing resources from a jar file.


Joined: Aug 28, 2001
Posts: 29

i am having the problem of accessing resources from the jar file. Here is a test class that i wrote to access conf.properties located in the jar. I tried the ones suggested in the other 'Threads' in this forum but still having the same problem.
package test;
public class Test{
public static void main(String args[]){
Test t1 = new Test();
java.net.URL url =t1.getClass().getResource("conf.properties");
if(url == null)
System.out.println("could'nt find the resource");
System.out.println("Yes..got it..."+url.toString());

I have conf.properties in the test directory i.e., same directory as the 'test' class.
Here is the output when i compile and create a jar file.
E:\projects\classes>jar cvf test.jar test
added manifest
adding: test/(in = 0) (out= 0)(stored 0%)
adding: test/dp.properties(in = 24) (out= 17)(deflated 29%)
adding: test/test.class(in = 784) (out= 470)(deflated 40%)
I get the following output.
Yes..got it...jar:file:/E:/projects/classes/test2.jar!/conf.properties
When i load this properties file i get a FileNotFoundException.
Whats up with the url string that i get from the getResource() method. Looks fishy to me...
Any help is greatly appriciated.
Thanks,-PC RE
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The answer is probably in the code that you don't quote: how are you loading the properties file? getResourceAsStream()? Something else?
- Peter

Joined: Aug 28, 2001
Posts: 29
u were right. i was using FileReader. When i used getResourceAsStream() it worked. Thanks man.
I agree. Here's the link: http://aspose.com/file-tools
subject: accessing resources from a jar file.
It's not a secret anymore!