Jerrin Thomas
Jerrin Thomas

Reputation: 289

How to get the path to a file in src/main/resources folder?

Assume standard maven setup. I have a file abc.conf in src/main/resources folder of my project. How do i get the absolute path to abc.conf in the code?

Upvotes: 1

Views: 4518

Answers (3)

Maven automatically sets the current working directory, then you can just use:

File resourcesDirectory = new File("src/main/resources/abc.conf");
    
System.out.println( resourcesDirectory.getAbsolutePath() ); //prints absolute path of your abc.conf

I hope it helps.

Upvotes: 0

ender1986
ender1986

Reputation: 105

When you run your app, you are not using the resource file in your code folder(src/main/resources) but the ones in the target folder([yourProject]\target as default) where you build to.The target folder path can be changed, it's not a absolute path.

So you can use a "absolute" path out of the content you run or use a "relative" path to the target folder.

Upvotes: 1

Curiosa Globunznik
Curiosa Globunznik

Reputation: 3205

You'll be able to elicit the path of the resource in the JAR, but the running program can't know, where your development repository is, unless you supply the information explicitly.

Upvotes: 1

Related Questions