dcsimg
Finding a File/Path anywhere on classpath
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   James_Asher
Posted On:   Monday, January 13, 2003 12:36 PM

I have a program that needs to find a file in an "earlier" directory. I know the code below is close, but there is still something wrong, but I dont' know what. Any help?

			
InputStream is = PKI.class.getClass().getResourceAsStream("/rsaPUB.txt");

this will get rsaPUB.txt if it's in the same directory but not in directories "above" it

			
InputStream is = PKI.class.getClass().getResourceAsStream("rsaPUB.txt");

won't get anything, it's that slash that helps, but still, i can't get anything above...

Re: Finding a File/Path anywhere on classpath

Posted By:   Lasse_Koskela  
Posted On:   Monday, January 13, 2003 01:51 PM

From java.lang.Class#getResourceAsStream(String) API documentation:


This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/".


You could try a couple of things.


First, you could try to change into


PKI.class.getClass().getClassLoader().getResourceAsStream("...");

which should prevent the path transformation performed by Class.getResourceAsStream().


Another thing to try could be to use
ClassLoader.getSystemResource(String), which (I suppose) tries to search using the classpath.


I hope at least one of these works. I haven't tried myself.

About | Sitemap | Contact