Creating native executables
After creating a runnable JAR file, it is possible to create a native executable from that JAR file using the command native-image -jar THE_JAR_FILE.jar.
Alternatively, it is possible to create a native image from a single class using the command native-image fully.qualified.name.of.MainClass in the classpath root.
Reflection and JNI configuration
If the compiled application uses Reflection at runtime, the reflectively accessed elements need to be known at compile-time. It is possible to specify them using by creating a JSON file listing these elements. This JSON file can then be passed to native-image using the argument -H:ReflectionConfigurationFiles=/path/to/reflect-config.json.
Such a file could look like the following:
[
{
"name" : "fully.qualified.name.of.SomeClass",
"methods" : [
{ "name" : "<init>", "parameterTypes" : [] }
]
},
{
"name" : "fully.qualified.name.of.OtherClass",
"methods" : [
{ "name" : "<init>", "parameterTypes" : ["java.lang.String"] },
{ "name" : "test", "parameterTypes" : ["java.util.List"] }
]
},
{
"name" : "fully.qualified.name.of.YetAnotherClass",
"allDeclaredMethods" : true,
"allDeclaredConstructors" : true,
"allDeclaredFields" : true
}
]
In this example, the no-args constructor of the class fully.qualified.name.of.SomeClass as well as the constructor fully.qualified.name.of.OtherClass taking a String argument, the method test(String) of the same class and all methods, fields and constructors of fully.qualified.name.of.YetAnotherClass can be accessed using Reflection.
If the application uses JNI and some Java classes/methods/constructors/fields need to be accessed from JNI, one needs to create a JSON file like the one for reflection and list all elements which should be accessible from JNI. This file can then be passed to native-image using -H:JNIConfigurationFiles=/path/to/jni-config.json.