我想访问当前运行的jar的资源文件夹中的一个文件。文件位于My_app.jar
内部,位于/apps/dashboard/
的位置我试着像这个一样访问它
String customScriptPath = "script/template.sh";
public String getTemplatePath() {
Resource temp= new ClassPathResource(this.customScriptPath, this.getClass().getClassLoader());
try {
File templateFile = temp.getFile();
logger.info("Script template path = "+templateFile.getAbsolutePath());
return templateFile.getAbsolutePath();
} catch (IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
return null;
}
我得到了这个错误
class path resource [script/template.sh] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/apps/dashboard/My_app.jar!/BOOT-INF/classes!/script/template.sh
不能使用File
访问template.sh
。File
用于引用文件系统中的文件。在您的案例中,您正试图引用jar文件中的某些内容。
如果您想阅读template.sh
的内容,请使用Resource.getInputStream()
获取流。若要记录文件的位置,请使用Resource.getURL()
。