【问题标题】:Missing properties file in Graal native imageGraal 本机图像中缺少属性文件
【发布时间】:2021-01-31 15:22:15
【问题描述】:

我将应用程序打包在 Graal 原生映像中。
我正在使用以下方式加载属性:

InputStream resourceAsStream = MainApplication.class.getResourceAsStream("/application.properties");

但是,当我尝试执行二进制文件时,我收到以下错误:

Exception in thread "main" java.lang.NullPointerException: inStream parameter is null

当项目被打包成原生镜像时,Graal 没有附加我的application.properties 文件。

我正在使用 Gradle 和 com.palantir.graal,设置如下:

graal {
    mainClass '<path-to-main-class>'
    outputName '<output-name>'
    javaVersion '11'
}

有没有办法可以使用build/resources 中的application.properties

【问题讨论】:

  • 看来你也有同样的问题:stackoverflow.com/questions/63609134/…
  • @ernest_k 解决方案有点过时,文档链接不正确。
  • The document it links to 似乎仅在六天前更新。但无论如何,我想你的判断力比我好。
  • 即使添加了这个option '-H:IncludeResources=\'.*/application.*properties$\'',我仍然遇到同样的错误。

标签: java gradle graalvm graalvm-native-image


【解决方案1】:

要将资源包含在本机映像可执行文件中,需要对其进行配置。静态分析不能自动包含它们。

资源的配置在docs中描述。

简而言之就是一个描述要包含的资源的json文件,例如:

{
  "resources": {
    "includes": [
      {"pattern": "<Java regexp that matches resource(s) to be included in the image>"},
      {"pattern": "<another regexp>"},
      ...
    ],
    "excludes": [
      {"pattern": "<Java regexp that matches resource(s) to be excluded from the image>"},
      {"pattern": "<another regexp>"},
      ...
    ]
  } 
}

创建此类配置的最佳方式可能是使用 javaagent 在 JVM 上运行您的应用程序,该 javaagent 将跟踪需要配置的所有内容并输出您可以包含到本机映像构建过程中的配置。

the docs 中描述了辅助配置 javaagent,但简而言之,您可以像这样运行应用程序:

java -agentlib:native-image-agent=config-output-dir=/path/to/config-dir/ -jar myapp.jar

然后使用应用程序执行代理将跟踪的代码路径。代理不会静态分析应用程序,因此需要使用它来提供有意义的配置。

【讨论】:

  • 它可以与我正在使用的 palantir-graal 插件一起使用吗?
猜你喜欢
  • 2020-12-10
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 2012-09-20
  • 2012-03-24
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多