After springboot labeled jar file failed to obtain the classpath

Original link: https: //blog.csdn.net/qq_18748427/article/details/78606432

After springboot labeled jar file failed to obtain the classpath

Use the following code:

ClassPathResource resource = new ClassPathResource("application.yml");
File file = resource.getFile();
FileUtils.readLines(file).forEach(System.out::println);

Can be obtained when the file is not packaged, the package error

Caused by: java.io.FileNotFoundException: class path resource [application.yml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/sunmnet/JetBrains/workspace/bigdata-parse-table/target/bigdata-parse-table-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/application.yml
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at hello.whz.Application.lambda$lookup$0(Application.java:30) [classes!/:1.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
... 14 common frames omitted

This is because packaged Spring attempts to access a file system path, but can not access the path of the JAR.
So you must use resource.getInputStream ()

ClassPathResource resource = new ClassPathResource("application.yml");
InputStream inputStream = resource.getInputStream();
IOUtils.readLines(inputStream).forEach(System.out::println);

 

Guess you like

Origin www.cnblogs.com/fswhq/p/10993583.html