Search code examples
javajava-8executable-jargraalvmgraalvm-native-image

Graalvm generated binary is working inside the project directory but not outside that directory


I am using a rather simple Java application that receives an input file, process it and generate an output file. I am using Picocli for arguments to the application and Graalvm to create a native binary. I first created the artifact in intellij including the libraries and specifying the class containing the main method, I then used build artifact option to build the jar file. After that, I used the following command to create the binary

native-image -jar metadata-processor.jar

The jar and binary are created inside the project as projectName/out/artifacts/app_jar I can run the binary using

./binary -i abc -o xyz

I can run the jar file using

java -jar binary.jar -i abc -o xyz

However when I move the binary to ~Download directory and run the same above command I get the error

./binary -i abc -o xyz
Error: Could not find or load main class Main

The jar is still working though i.e.

java -jar binary.jar -i abc -o xyz

If I create a simple hello world program and create a binary then it works fine even if I move it to another folder so it seems to be a library issue or project structure.

Please let me know if some special step is required in the case of libraries.


Solution

  • Okay, finally, what worked for me is to create the configuration file. It becomes a two-step process

    Step 1:

    First I create a jar file of the project using intellij then I run the jar file to create a configuration file of the project in META_INF/native-image directory using -agentlib:native-image-agent i.e.

    java -agentlib:native-image-agent=config-output-dir=/Users/mianahmad/goldeneye/json-processor/src/main/resources/META-INF/native-image -jar metadata-processor.jar -i /Users/mianahmad/goldeneye/json-processor/input/Claimant.json -o /Users/mianahmad/goldeneye/json-processor/output/Claimant_output.json
    

    Step 2:

    After that I created a jar again and then created the native image.

    native-image -jar app-1.0-SNAPSHOT-jar-with-dependencies.jar
    

    After that, I moved the project to another location using

    mv app-1.0-SNAPSHOT-jar-with-dependencies /Users/mianahmad/Desktop
    

    Run the binary and viola it is still working

    ./app-1.0-SNAPSHOT-jar-with-dependencies