Missing properties file in Graal native image

1.7k views Asked by At

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

There are 1 answers

1
Oleg Šelajev On

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.