• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best place for .properties file?

 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\db.properties" 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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
-arun
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dave
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David!
It still does'nt work. Here is my directory structure
%CATALINA_ROOT%\webapps\myapp
my jsps and HTML are in myapp folder
The \WEB-INF\classes\conf folder has my db.properties 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 org.xxx.web;
The bean in this package says it cannot find the db.properties 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

-thanks
Sudharsan
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
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.
k
 
Tina Coleman
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
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
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karl!
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic