0

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.

Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
  • It looks like native-image is failing to generate a binary and is falling back to JVM-mode. Try compiling with `--no-fallback` and the build will probably fail. – zakkak May 03 '23 at 12:54
  • @zakkak yes it failed with some type error `Error: type is not available in this platform: org.graalvm.nativeimage.impl.ImageSingletonsSupport` – Dr. Mian May 03 '23 at 12:56
  • Is it because of the mac m1 max or some other issue? – Dr. Mian May 03 '23 at 12:57
  • 2
    I am not sure to be honest. Better update the github issue and provide more info there (like the full stack trace). – zakkak May 04 '23 at 14:01

1 Answers1

2

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
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69