Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
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 The Software Craftsman this week in the Agile forum!
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 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(); url =t1.getClass().getResource("");
if(url == null)
System.out.println("could'nt find the resource");
System.out.println(" it..."+url.toString());

I have 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/ = 24) (out= 17)(deflated 29%)
adding: test/test.class(in = 784) (out= 470)(deflated 40%)
I get the following output. it...jar:file:/E:/projects/classes/test2.jar!/
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.
Have you tried LearnNowOnline?
subject: accessing resources from a jar file.