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

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

我在命令下运行

java -jar mailer.jar

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

http://img.mukewang.com/61d6bac80001a2e615950733.jpg

白衣非少年
浏览 435回答 3
3回答

catspeake

因为当打包的 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();}

烙印99

试试这个IOUtils.toString(new InputStreamReader(this.getClassLoader().getResourceAsStream("fileName.json")))ORnew InputStreamReader(new ClassPathResource("fileName.json", this.getClassLoader()).getInputStream())不要使用FileReader或File使用InputStreamReader , InputStream等

呼唤远方

似乎应用程序正在尝试通过 AbstractFileResolvingResource.getFile()(堆栈跟踪中的几行)访问文件,这在可运行的 Spring Boot jar 中是不可能的(从 IDE 运行时它可能工作)。尝试使用 getInputStream() 代替,例如参见这篇文章。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java