首页 > 解决方案 > 无法在 jar 文件(docker build)中加载资源文件。本地工作正常

问题描述

我在 jar 中有一个带有路径的证书文件-BOOT-INF/classes/keystore.jks。我已经使用classpath:keystore.jksResource 来访问该文件。在本地,我能够获取该文件并能够运行 Spring Boot 应用程序。但是当我构建它并创建一个 jar 时,我可以看到该文件,但我遇到了异常 -

FileNotFoundException: file:/libs/app.jar!/BOOT-INF/classes!/keystore.jks (No such file or directory)\n\tat j.io.FileInputStream.open0(FileInputStream.java)\n\tat j.io.FileInputStream.open(FileInputStream.java:219)\n\tat j.io.FileInputStream.<init>(FileInputStream.java:157)\n\tat c.s.i.o.c.f.HttpConnectionFactoryImpl.loadKeystore(HttpConnectionFactoryImpl.java:119)\n\t... 55 common frames omitted\nWrapped by: c.s.i.o.c.e.SSLContextException: Unable to load keystore / truststore /file:/libs/app.jar!/BOOT-INF/classes!/keystore.jks

我尝试了以下不起作用的东西 -

storePath = ((ClassPathResource)ebbsTrustStore).getPath()
  path = CheckKeytsotrePath.getInternalResource(storePath).getPath()
  ResourceUtils.getFile("classpath:keystore.jks").getPath()
((ClassPathResource)ebbsTrustStore).getURL().toString()

我需要将此路径传递给库方法,该方法是BOOT-INF/lib同一个 jar 中的一部分。如果您对此有任何解决方案/提示,请告诉我。

标签: javaspring-bootresourcesclasspathjks

解决方案


推荐阅读