Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导应用程序无法检测类路径中存在的文件_Java_Spring Boot_Docker - Fatal编程技术网

Java Spring引导应用程序无法检测类路径中存在的文件

Java Spring引导应用程序无法检测类路径中存在的文件,java,spring-boot,docker,Java,Spring Boot,Docker,我在spring boot应用程序的application.yml文件中给出了以下内容 file.path:classpath:input.txt 我已将input.txt放在src/main/resources下 当我在docker中部署应用程序时,出现以下异常 原因:java.io.FileNotFoundException:class path资源[input.txt]无法解析为绝对文件路径,因为它不在文件系统:jar:file:/app/app.jar中/BOOT-INF/classes

我在spring boot应用程序的application.yml文件中给出了以下内容

file.path:classpath:input.txt

我已将input.txt放在src/main/resources下

当我在docker中部署应用程序时,出现以下异常

原因:java.io.FileNotFoundException:class path资源[input.txt]无法解析为绝对文件路径,因为它不在文件系统:jar:file:/app/app.jar中/BOOT-INF/classes/input.txt


但当我从本地运行它时,文件正在加载。请建议一种修复方法。

您必须在Spring中使用以下代码读取该文件

File rese = new ClassPathResource("input.txt").getFile();
String contents = new String(Files.readAllBytes(res.toPath()));

根据您的回答,我注意到您正在加载文件,如下所示:

@Value(“${file.path}”)//classpath:input.txt
私有字符串文件路径;
私有void readFile(){
新文件(文件路径);
}
要使其工作,您可以这样使用它:

@Value(“${file.path}”)
私有org.springframework.core.io.Resource文件;
私有void readFile(){
InputStream=file.getInputStream();
//你的密码在这里
}

显示有关如何读取文件的代码。@Value(${file.path}”)私有字符串filepath;以及在使用文件f=新文件(文件路径)访问文件的方法中;似乎您正试图将input.txt作为jar的一部分,当运行jar时,它应该从内部解析。您能检查一下在BOOT-INF/classes下jar中是否有input.txt文件吗?如果您可以将input.txt放在jar之外,并将该文件夹放在类路径上,那就更好了。您不能这样使用。@AshokPrajapati,在我们的项目中,我们不是管理dockers,我们将把jar传递给团队,他们将进行部署。所以文件应该是我们jar的一部分。我可以看到文件在BOOT-INF/classes下。我可以从命令提示符测试应用程序。当它部署在docker中时,面对这个问题