运行 Wildfly inside Docker 时类加载器 getResource 失败

Classloader getResource failed when running Wildfly inside Docker

我在 Wildfly 上部署了一个 JavaEE 应用程序,运行 在 Ubuntu 和 Windows OS 上都正确地部署了。但是,当我尝试 Docker 化应用程序时,它失败了。

这是代码中失败的部分:

File templateFile = new File(ESGenerator.class.getClassLoader().getResource("/endpoint-js-template/get-template.js").getFile());
// ...
endpointJSInterface.setTemplate(FileUtils.readFileToString(templateFile));

这是我尝试登录时的文件位置:

// 好的 - Ubuntu /home/czetsuya/java/jboss/wildfly-15.0.1.Final/standalone/deployments/myApp.war/WEB-INF/lib/myApp-admin-ejbs-6.9.0-SNAPSHOT.jar/endpoint-js-template/get-template.js

// 好的 - Windows c:\Java\Jboss\wildfly-15.0.1.Final\standalone\deployments\myApp.war\WEB-INF\lib\myApp-admin-ejbs-6.9.0-SNAPSHOT.jar\endpoint-js-template\get-template.js

// 柯 - Docker /content/myApp.war/WEB-INF/lib/myApp-admin-ejbs-6.9.0-SNAPSHOT.jar/endpoint-js-template/get-template.js

“/content”从何而来?解决此问题的最佳方法是什么?

解决方案是避免使用新的文件操作。

这是我解决这个问题的方法。

// get the URL of the resource from the jar file
URL templateUrl = ESGenerator.class.getClassLoader().getResource("/endpoint-js-template/get-template.js");
// load as string using IOUtils
endpointJSInterface.setTemplate(IOUtils.toString(templateUrl));