wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to get the path of property 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 » Beginning Java
Bookmark "How to get the path of property file." Watch "How to get the path of property file." New topic

How to get the path of property file.

Edward Durai
Ranch Hand

Joined: Oct 09, 2004
Posts: 223

I have one sample project named as TMS, In my "WEB-INF/conf" foler contains database.properties file. It contains

hostname = some host name
url= some url

This project contains only one database.properties file.
In java file, I want to get hostname and url from that property file.


Properties props=new Properties () ;
props.load ( new FileInputStream ( new File ("E:\\Workspace\\TMS\\WebRoot\\WEB-INF\\conf\\database.properties"))) ;

String host= "hostname";
String val = props.getProperty ("Host Name Is :"+ host ) ;

Using the above code, I have got both hostname and url.

But my problem is, How to get the current project database.properties file path.

Because in the above path name is hard coded.

So How we get current project database.properties file path?

Thanking you.

Thank You<br />Edward
Francois Dang Ngoc

Joined: Sep 02, 2007
Posts: 6
Hi Edward,

You can use the method getServletContext() as follows:

InputStream is = getServletContext().getResourceAsStream ("/WEB-INF/conf/database.properties");
Properties props = new Properties();
out.println("HOST: " + props.getProperty("host") + " URL: " + props.getProperty("url"));

Hope it helps,


Shane W
Ranch Hand

Joined: Aug 31, 2007
Posts: 30
How about using ResourceBundle?
You just need to know the property file name.

Apple Man
It is sorta covered in the JavaRanch Style Guide.
subject: How to get the path of property file.
Similar Threads
deploying in tomcat 6
Want to change Application Property at startup
tomcat connection pooling
NX: suncertify.properties
Accessing a context.xml from a virtualhost