dev777
dev777

Reputation: 1019

How to get the current project path from eclipse using java

I have a project named SampleProj in eclipse.

The project structure is mentioned as below :

SampleProj
   |  
    -- META-INF
            |
             -- dbdetails.properties

Currently,I'm reading the file like this :

FileReader reader = new FileReader("C://Workspace//SampleProj/META-INF//dbdetails.properties");

But this path is local to my system rit. So, I want to make this path generic.

So, I'm trying to keep the path like this :

FileReader reader = new FileReader("SampleProj/META-INF//dbdetails.properties");

But,I'm getting file not found exception.

How can I read this file from Project. Can anyone please help me out regarding this ...

Upvotes: 3

Views: 13335

Answers (4)

user3077931
user3077931

Reputation: 11

IF your using spring web-mvc project use this samplecode:

String path=session.getServletContext().getRealPath("/ui_resources/images/logo.png");

Upvotes: 0

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

  • Go to Run/Run Configurations...
  • Select the Launch Configuration that you want to use
  • Select the tab Arguments
  • In the Working directory section, click Other
  • Put C:\Workspace in the text field
  • Click Apply

Then SampleProj/META-INF/dbdetails.properties should work as path since a relative path is relative to the user working directory corresponding to the System property user.dir.

Upvotes: 1

Abhijeet
Abhijeet

Reputation: 4309

You can retrieve using :

String workingDir = System.getProperty("user.dir");

Then you can do it like this :

FileReader reader = new FileReader(workingDir +"//SampleProj//META-INF//dbdetails.properties");

Upvotes: 4

Jekin Kalariya
Jekin Kalariya

Reputation: 3507

As fas as your development is concerned you can do it like below but once you deploy the project its better to add file in classpath

String currentDirectory=System.getProperty("user.dir");


FileReader reader = new FileReader(currentDirectory+"/SampleProj/META-INF//dbdetails.properties");

Upvotes: 0

Related Questions