Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing resources from a jar file.

 
PC RE
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peter,
u were right. i was using FileReader. When i used getResourceAsStream() it worked. Thanks man.
-PC RE
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic