I have spring project. My file here: /src/main/java/resources/file.txt.
How I can get full path of this?
If I run from test it works:
String path = System.getProperty("user.dir") + "/src/main/java/resources/file.txt";
But if I use tomcat, it shows another path inside tomcat folder. So spring cannot find this path.
EDIT: this.getClass().getClassLoader().getResource("file.txt") is null
UPDATED: sorry for stupid question. It works, I set wrong name of file.
As file - file.txt is in resources directory, normally this would be copied to class-path by build process(tool like Maven, Gradle). And this would easy with to load file with relative path.
This thread had extensively talked about how to load file from class-path.
How to really read text file from classpath in Java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With