File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "accessing resources from a jar file." Watch "accessing resources from a jar file." New topic
Author

accessing resources from a jar file.

PC RE
Greenhorn

Joined: Aug 28, 2001
Posts: 29

Hi,
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");
else
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
author
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
PC RE
Greenhorn

Joined: Aug 28, 2001
Posts: 29
peter,
u were right. i was using FileReader. When i used getResourceAsStream() it worked. Thanks man.
-PC RE
 
wood burning stoves
 
subject: accessing resources from a jar file.
 
Similar Threads
setup not running
SCJD- problem making jar - project from Andrew Monkhouse and Terry Camerlengo book
Problem with jar
executing jar file
java -jar