0
votes

I have the application packed in Graal native image.
I'm loading the properties using:

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

However, when I try to execute the binary, I'm getting the following error:

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

Turns out that Graal is not attaching my application.properties file when the project is packed into native image.

I'm using Gradle and com.palantir.graal with the following settings:

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

Is there a way I can use application.properties from build/resources?

1
Seems like you have the same problem as this: stackoverflow.com/questions/63609134/…ernest_k
@ernest_k The solution is a bit outdated, link to the documentation is incorrect.Forin
The document it links to seems to have been updated only six days ago. But anyway, I guess you can judge better than I.ernest_k
Even with adding this option '-H:IncludeResources=\'.*/application.*properties$\'' I'm getting still the same error.Forin

1 Answers

1
votes

For resources to be included into the native image executable they need to be configured. Static analysis cannot include them automatically.

The configuration for the resources is described in the docs.

In short it's a json file describing the resources to be included, for example:

{
  "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>"},
      ...
    ]
  } 
}

Probably the best way to create configuration like this is to run your application on the JVM with a javaagent that would trace all things needed to be configured and output the configuration that you can include into the native image build process.

The assisted configuraion javaagent is described in the docs, but in a nutshell you run your application like:

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

And then use the app to execute to code paths which the agent will trace. The agent doesn't statically analyze the application, so it needs to be used to provide meaningful config.