【发布时间】:2020-11-26 10:48:51
【问题描述】:
我想访问当前运行的 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
【问题讨论】:
-
看看一个叫做:
getResourceAsStream的东西。在这里您可以找到将来自JAR的文件提取到文件系统中的示例。在您的情况下,您可以简单地阅读它:github.com/mkowsiak/jnicookbook/blob/master/recipes/recipeNo035/… - 看看方法:extractLibrary
标签: java spring-boot file jar