springboot ClassPathResource报找不到文件

代码如下

ClassPathResource resource = new ClassPathResource("/home/tomcat/apache-tomcat-9.0.43/tempPositionStr.properties");

File file = new File(String.valueOf(resource.getFile()));

报错如下:

java.io.FileNotFoundException: class path resource [home/tomcat/apache-tomcat-9.0.43/tempPositionStr.properties] cannot be resolved to URL because it does not exist

 

从报错的路径来看,home目录前面的斜杠被忽略了?这是为什么?

1、把斜杠去掉看看
2、核对下路径没有没写对

 

这个是你路径写错了 没有显示斜杠不一定是没有多读取到 也有可能是日志没有打印完整 你是在虚拟机上面跑的项目嘛

使用相对路径