Home arrow static arrow Java Programming [Archive] - Can't find properties file
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Can't find properties file
This topic has 4 replies on 1 page.

Posts:26
Registered: 00-03-13
Can't find properties file  
Jul 22, 2004 12:27 PM



 
Hi, guys. I've been working all day trying to get my little app to find a properties file that I have. It's in both my source directory, and in the classes directory. I try to get it like:
InputStream in = ClassLoader.getSystemResourceAsStream("user.properties");

Since that file is where the .class file is, what am I missing when my code can't find that property file?

Thanks!
Jonathan
 

Posts:24,036
Registered: 2/3/03
Re: Can't find properties file  
Jul 22, 2004 12:31 PM (reply 1 of 4)



 
try:
InputStream in = YourClassName.class.getResourceAsStream("user.properties");
 

Posts:26
Registered: 00-03-13
Re: Can't find properties file  
Jul 22, 2004 12:34 PM (reply 2 of 4)



 
Right after I typed the message, I added the file's directory like:

jab/user.properties


and that worked. The directory the code is in is c:\WindowResizer\src\jab, but the classpath for the source is c:\windowsresizer\src. So, adding the jab before user.properties finally got it working.

Thanks, though!
 

Posts:21,719
Registered: 98-02-20
Re: Can't find properties file  
Jul 22, 2004 12:36 PM (reply 3 of 4)



 
Is this a desktop or a Web app?

Web apps use a hierarchy of class loaders.

Try this and see if it works better:

            ClassLoader classLoader           = Class.forName(CLASS_NAME).getClassLoader();            URL propertiesURL                       = classLoader.getResource(propertiesFileName);            InputStream propertiesStream    = propertiesURL.openStream();            Properties properties                     = new Properties();            properties.load(propertiesStream);            propertiesStream.close();


Make sure the path to the .properties file is in your CLASSPATH, of course. AND import java.net.URL.

%
 

Posts:24,036
Registered: 2/3/03
Re: Can't find properties file  
Jul 22, 2004 12:37 PM (reply 4 of 4)



 
Right after I typed the message, I added the file's
directory like:

jab/user.properties


and that worked. The directory the code is in is
c:\WindowResizer\src\jab, but the classpath for the
source is c:\windowsresizer\src. So, adding the jab
before user.properties finally got it working.

Thanks, though!

No problem. You may want to read the following for more information: [url=http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html]Smartly load your properties[/url]
 
This topic has 4 replies on 1 page.