我将应用程序打包在 Graal native 镜像中。
我正在使用以下方法加载属性:
InputStream resourceAsStream = MainApplication.class.getResourceAsStream("/application.properties");
但是,当我尝试执行二进制文件时,出现以下错误:Exception in thread "main" java.lang.NullPointerException: inStream parameter is null
原来 Graal 没有附加我的 application.properties
项目打包到 native 镜像时的文件。我正在使用 Gradle 和
com.palantir.graal
使用以下设置:graal {
mainClass '<path-to-main-class>'
outputName '<output-name>'
javaVersion '11'
}
有什么方法可以使用application.properties
来自 build/resources
?
最佳答案
要将资源包含到 native 镜像可执行文件中,需要对其进行配置。静态分析不能自动包含它们。
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 将跟踪所有需要配置的内容并输出您可以包含到 native 镜像构建过程中的配置。the docs 中描述了辅助配置 javaagent ,但简而言之,您可以像这样运行您的应用程序:
java -agentlib:native-image-agent=config-output-dir=/path/to/config-dir/ -jar myapp.jar
然后使用该应用程序执行代理将跟踪的代码路径。代理不会静态分析应用程序,因此需要使用它来提供有意义的配置。
关于java - Graal native 图像中缺少属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64400417/