首页 > 解决方案 > Spring Boot uber JAR:无法解析为绝对文件路径,因为它不驻留在文件系统中

问题描述

将 Spring Boot 版本 1.5.7.RELEASE 导出到可运行 JAR 后出现以下错误。由于安全原因,我不使用 maven,并且我在构建路径中添加了所有 JAR。

我在命令下面运行

java -jar mailer.jar

然后我收到了您在屏幕截图中看到的错误

在此处输入图像描述

标签: javaspring-boot

解决方案


因为当你的资源在打包的 uber-jar 中不存在时,类路径有问题。使用这样的解决方案

String fuu = "";
ClassPathResource classPathResource = new ClassPathResource("static/foo.txt");
try {
    byte[] binaryData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
    fuu = new String(binaryData, StandardCharsets.UTF_8);
} catch (IOException e) {
    e.printStackTrace();
}

推荐阅读