File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Best place for .properties file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Best place for .properties file?" Watch "Best place for .properties file?" New topic

Best place for .properties file?

Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
I have some classes packages in a .jar and put in the myApp\WEB-INF\lib folder of TomCat. These classes read a properties file which will be placed under a conf folder. Fo ex., they should read a file "\conf\" to get some info. When i created a 'conf' folder under the lib directory, the classes does'nt read from there. Should i package them along with the .jar. But I wanted it to be editable any time.. Any idea where the file should be?
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
You can put the file in ther server context and in your code make sure the the file is read from server context. and place your prop file there.

Sharing is learning
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

If you want to threat a file on the file syatem the same as you have it in the jar, you need to place it in the myApp\WEB-INF\classes\conf\ directory
The ClassLoader is designed so that it only loads jars from the lib directory, not other files.
Also be aware that while this is something I do often, it ony works for properties used in pure web-apps. If you have a distributed app with EJB components etc you'l have to find another solution, since these won't be able to see the properties file.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Okay! But if I have my jar in the myApp\WEB-INF\lib folder and the properties file in the myApp\WEB-INF\classes\conf folder, is it OKay?
Michael Mendelson
Ranch Hand

Joined: Dec 19, 2000
Posts: 73
Sure it's ok! It's actually a good practice to keep your configuration info (in the properties file) separate from the code. That way you can deploy the same code to a production or deployment site, and leave the config files there.
Some people achieve this using ant to actually construct the prop files, but I don't. Just lazy I guess.
Wherever you put them, you need to make sure your app can find them. What I usually do (to prevent hard-coding of these paths) is to use a properties file reader class that searches the classpath for the given file. It also checks every few seconds to see if the file date has changed, and reloads if appropriate. That way I don't need to restart tomcat for config changes.
Let me know if you want some code!
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
It still does'nt work. Here is my directory structure
my jsps and HTML are in myapp folder
The \WEB-INF\classes\conf folder has my file.
The \WEB-INF\lib folder contains myapp.jar and the Connector/J mySQL driver jar. The myapp.jar contains classes inpackage structure similar to this;
The bean in this package says it cannot find the file and raises FileNotFoundException.
Here is that code snippet

This is my URL and Driver Class
URL : mysql:jdbc://localhost:3306/tamilini?user=tamuser
Driver : com.mysql.jdbc.Driver

And this is the Exception

karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
the exception you showed is not from a missing file but from a missing jdbc driver. make sure your jdbc driver is available to the servlet (or the webserver, depending on how you use it)
for the properties file: create a File object with your file name and printout the absolute path to see what file you are trying to read.
Tina Coleman
Ranch Hand

Joined: Dec 12, 2001
Posts: 150
I'd like to see that code for the dynamic properties setup. Please send it to me privately, if it's large, so as to not clog up the list.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Sorry! I pasted a wrong Exception code. But that is my second problem. My JDBC driver jar is in the \myapp\WEB-INF\lib\ folder. My program fails to recognize it.
I agree. Here's the link:
subject: Best place for .properties file?
It's not a secret anymore!