fast
fast

Reputation: 61

Spring boot can not recognize path of my image

I have a spring boot program, and in the data loader class that implements commandLiner, I saved a picture of roastedChicken, which it's path that I entered is \static\images\roastedChicken.jpg, and you can see it in detail in the image below. But the program gives an error and says that it does not recognize this path, why? image

error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-07-25 23:21:08.892 ERROR 2632 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:774) ~[spring-boot-2.7.1.jar:2.7.1]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) ~[spring-boot-2.7.1.jar:2.7.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.7.1.jar:2.7.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.1.jar:2.7.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.1.jar:2.7.1]
    at com.arbabsoft.recipe.RecipeApplication.main(RecipeApplication.java:9) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.7.1.jar:2.7.1]
Caused by: java.nio.file.NoSuchFileException: \static\images\roastedChicken.jpg
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85) ~[na:na]
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103) ~[na:na]
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108) ~[na:na]
    at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:235) ~[na:na]
    at java.base/java.nio.file.Files.newByteChannel(Files.java:371) ~[na:na]
    at java.base/java.nio.file.Files.newByteChannel(Files.java:422) ~[na:na]
    at java.base/java.nio.file.Files.readAllBytes(Files.java:3206) ~[na:na]
    at com.arbabsoft.recipe.bootstrap.LoadData.run(LoadData.java:29) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-2.7.1.jar:2.7.1]
    ... 10 common frames omitted

Upvotes: 0

Views: 277

Answers (1)

Pp88
Pp88

Reputation: 1082

you can use:

ResourceUtils.getFile("classpath:static/images/roastedChicken.jpg");

Upvotes: 1

Related Questions