在Docker中运行Wildfly时,Classloader getResource失败。

问题描述 投票:0回答:1

我有一个JavaEE应用程序部署在Wildfly上,并在Ubuntu和Windows操作系统上正常运行。然而,当我尝试将应用程序Docker化时,却失败了。

以下是失败的部分代码。

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

以下是我尝试记录时的文件位置。

/ Ok - Ubuntuhomeczetsuyajavajbosswildfly-15.0.1.FinalstandalonedeploentsmyApp.warWEB-INFlibmyApp-admin-ejbs-6.9.0-SNAPSHOT.jarendpoint-js-templateget-template.js。

/ Ok - Windowsc:/Java/Jboss/wildfly-15.0.1.Final/standalone/deploentsym/myApp.war/WEB-INFlib\myApp-admin-ejbs-6.9.0-SNAPSHOT.jar\endpoint-js-template/get-template.js

/ Ko - DockercontentmyApp.warWEB-INFlibmyApp-admin-ejbs-6.9.0-SNAPSHOT.jarendpoint-js-templateget-template.js。

'内容'从何而来,解决这个问题的最好方法是什么?

docker wildfly classloader java-ee-6 java-ee-7
1个回答
0
投票

解决方案是避免使用new File操作。

下面是我如何解决这个问题。

// 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));
© www.soinside.com 2019 - 2024. All rights reserved.