简体   繁体   中英

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?

您将能够导出 JAR 中资源路径,但运行程序无法知道您的开发存储库在哪里,除非您明确提供信息。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM