简体   繁体   English

GraalVM native-image 如何从 Jar 读取资源文件

[英]GraalVM native-image how to read resource file from Jar

I have a jar file which contain some text file inside, I am trying to load the file as:我有一个 jar 文件,其中包含一些文本文件,我正在尝试将文件加载为:

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);

It doesn't work as error showing :它不能作为错误显示:

[SUB] java.lang.NullPointerException
[Thu Aug 27 12:07:48 SGT 2020][INFO] [SUB]  at java.io.Reader.<init>(Reader.java:167)
[Thu Aug 27 12:07:48 SGT 2020][INFO] [SUB]  at hellofx.HelloFX.readFileAsStringFromJar(HelloFX.java:116)
[Thu Aug 27 12:07:48 SGT 2020][INFO] [SUB]  at hellofx.HelloFX.test(HelloFX.java:107)

If I try to extract the resource file into classpath src/main/resources , then it is working fine.如果我尝试将资源文件提取到类路径src/main/resources ,那么它工作正常。

My question is, could we read resource file from Jar (when running in GraalVM native-image)?我的问题是,我们可以从 Jar 中读取资源文件吗(在GraalVM本机映像中运行时)? There are plenty of third party Java libraries which are reading the resource files that are bundled together in the same Jar, how could we overcome this?有很多第三方 Java 库正在读取捆绑在同一个 Jar 中的资源文件,我们如何克服这个问题?

PS update: PS更新:

it is indeed my mistake, confused with class.getResource() and class.getClassLoader().getResource() .这确实是我的错误,与class.getResource()class.getClassLoader().getResource()混淆。 One require slash in the beginning and another doesn't allow.一个在开头需要斜杠,另一个不允许。 Once I removed the slash in path variable, it is working fine.一旦我删除了path变量中的斜杠,它就可以正常工作。

You need to tell native image what resources to include via -H:IncludeResources=path .您需要通过-H:IncludeResources=path告诉原生图像要包含哪些资源。 The value can be a regular expression.该值可以是正则表达式。

See the documentation for more details: https://github.com/oracle/graal/blob/master/substratevm/Resources.md有关更多详细信息,请参阅文档: https : //github.com/oracle/graal/blob/master/substratevm/Resources.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM