wood burning stoves 2.0*
The moose likes Servlets and the fly likes problem with path for properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with path for properties file" Watch "problem with path for properties file" New topic

problem with path for properties file

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
Hello All,
not sure if this question goes here or in java for beginners but here goes
I've got a couple of pieces of info that will change when I move my servlet from a development environment to production. Things like the URL and database connection info etc. I'd like to store this stuff in a properties file rather than hardcode them into the servlet. If I read the properties file like this
properties.load(new FileInputStream("properties.file"));
it has to be in my home directory (I'm running linux)
I want to use a relative path so it works when I move around to different enviroments. Where should the file be if I specify it like this
properties.load(new FileInputStream("./properties.file"));
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Always use classpath-based URLs, not directory-based ones.
URL myPropsURL = getClass().getResource("/myprops.properties");
The above finds myprops.properties rooted in the classpath.
Use the URL instead of a path name in APIs that allow either.
Or, to open the stream directly:
InputStream myPropsStream = getClass().getResourceAsStream("/myprops.properties");
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
got an answer in 8 minutes
Thanx Billy
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: problem with path for properties file
Similar Threads
Need an idea to improve design...
Reading properits file from an ejb
Can't find logj4.properties using netbeans, hibernate and glassfish
Accessing Files on File System in J2EE and WSAD 5.0
Error reading properties file