dunfa
dunfa

Reputation: 539

How to determine a file path in Maven project?

I am using Maven for a project. In one of my java class, I need to grab a property file. I cannot use absolute file location but relative path. I tried several ways but no luck.

The java is in

myProject/src/main/java/com/some/myConfiguration.java

The property file is in

myProject/src/main/com/some/resources/myProperties/myFeatures.properties

And in my java, I have some code like this

String filePath = "correct/path/to/myFeatures.properties";
File repositoryFile = new File(filePath);

So my problem is what I should give to filePath

Thank you for any advice.

Upvotes: 6

Views: 14496

Answers (2)

Adam Siemion
Adam Siemion

Reputation: 16039

  • put your myFeatures.properties property file inside src/main/resources as suggested by Maven's Standard Directory Layout

  • access it in your Java code using:

    this.getClass().getClassLoader().getResourceAsStream("myFeatures.properties");

Upvotes: 6

garyzhu
garyzhu

Reputation: 13

You can try this.getClass.getResource("/myProperties/myFeatures.properties") because the compiled path of resources is under the class path.

Upvotes: 0

Related Questions