Narayan
Narayan

Reputation: 1269

To get the root path of file in java

I am using eclipse Indigo IDE ..I am developing a jsp application (myProject) using eclipse in which i have a property file (myProject\webcontent\db.properties) for configuring database credentials.

I am trying to access this file from a class (myProject\src\samplePackage\sampleDBConnect.java). I have exported samplePackage.jar into *myProject\webContent\WEB-INF\lib*.

I have a Test.jsp page which calls a method in sampleDBConnect.java.

When i am trying to run this Test.jsp page, the current directory path shows C:\Documents and Settings\username. I have my project in some other drive(E:).

Can someone tell me how to access the properties file....

Upvotes: 1

Views: 1786

Answers (2)

mprabhat
mprabhat

Reputation: 20323

getClass().getResourceAsStream("db.properties");

This assumes your db.properties is in the same directory "samplePackage".

If you want to keep it in src directory or say resource directory then use

getClass().getClassLoader().getResourceAsStream("db.properties");

Since your java code belongs to the same web project you don't have to create a jar of the same project and place that in the WEB-INF/lib folder

Upvotes: 3

Liu guanghua
Liu guanghua

Reputation: 991

move db.properties to myProject\src dir, java code: InputStream input = sampleDBConnect.class.getClassLoader().getResourceAsStream("db.properties");

Upvotes: 1

Related Questions