我要访问当前正在运行的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
答案 0 :(得分:0)
您不能使用File
访问template.sh
。 File
用于引用文件系统中的文件。就您而言,您正在尝试引用jar文件中的某些内容。
如果您想阅读template.sh
的内容,请使用Resource.getInputStream()
进行流媒体播放。如果要记录文件的位置,请使用Resource.getURL()
。