java - Spring Boot uber JAR:无法解析为绝对文件路径,因为它不驻留在文件系统中
问题描述
将 Spring Boot 版本 1.5.7.RELEASE 导出到可运行 JAR 后出现以下错误。由于安全原因,我不使用 maven,并且我在构建路径中添加了所有 JAR。
我在命令下面运行
java -jar mailer.jar
然后我收到了您在屏幕截图中看到的错误
解决方案
因为当你的资源在打包的 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();
}
推荐阅读
- regex - 正则表达式 nginx 删除尾部斜杠不适用于 2 个文件夹?
- docker - 如何在 Dockerfile 中使用 git url 作为基础镜像?
- python - 使用 Python 根据两个 CSV 文件的列值过滤 CSV 文件中的行
- android - 如何使用数据绑定调用静态方法?
- networking - 查找collectd客户端的IP地址
- azure - Bot Builder:如何在 sharepoint 中使用 OAuth 2 隐式授权流方法创建机器人
- r - R:如何随机创建相等的变量块?
- html - 左侧和右侧具有固定宽度列的引导行
- sql - SQL SERVER:我想在删除第一列之前将我的数据传输到同一张表的另一列
- tablesorter - Tablesorter 计算数据上的自定义方程