Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file from resources by relative path (Spring)

Tags:

java

spring

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.

like image 334
annoirq Avatar asked Nov 14 '25 12:11

annoirq


1 Answers

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

like image 172
Subhrajyoti Majumder Avatar answered Nov 17 '25 09:11

Subhrajyoti Majumder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!