I have the following code (reduced to a minimum working example):

(ns timer.core (:gen-class))

(defn handle2 [x]
  (if (string? x) (Integer/parseInt x) x))

(defn -main
  ([max-time]
   (println (format "Going to sleep %d seconds" (handle2 max-time)))  
   (let [sleep-time (* (handle2 max-time) 1000)]  
     (Thread/sleep sleep-time)  
     (println (format "Waken up"))))  
  ([] (-main 10)))  

that works in the cider REPl on emacs and also works with > lein run on a windows terminal (x64 Native Command Prompt for VS 2022) and also can be compiled into an uberjar with leiningen and calling the java -jar command works. All the previous work with or without the max-time argument, as it is expected.

But when I build a executable from the uberjar with the GraalVM using the command

>  native-image --report-unsupported-elements-at-runtime --initialize-at-build-time -jar .\target\timer-0.1.0-SNAPSHOT-standalone.jar -H:Name=timer-cli

although it compiles a timer-cli.exe executable binary, this fails when is run with this error:

exp-timer-cli.exe
Going to sleep 10 seconds
Exception in thread "main" java.lang.IllegalArgumentException: No matching method sleep found taking 1 args
        at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:127)
        at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:332)
        at timer.core$_main.invokeStatic(core.clj:72)
        at timer.core$_main.invoke(core.clj:68)
        at timer.core$_main.invokeStatic(core.clj:76)
        at timer.core$_main.invoke(core.clj:68)
        at clojure.lang.AFn.applyToHelper(AFn.java:152)
        at clojure.lang.AFn.applyTo(AFn.java:144)
        at timer.core.main(Unknown Source)

with or without parameter. But if I substitute (handle2 max-time) by an actual number (let’s say 2), the program works, but of course with the hardcoded variable as sleep time, and prints the parameter value on the message string.

Does any one have experience with the GraalVM and knows why this happens? And how to solve this would be greatly appreciated.

Using:

  • Leiningen 2.10.0 on Java 19.0.2 OpenJDK 64-Bit Server VM
  • GraalVM 22.3.1
  • hunajakettuOPB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Ok, this worked, but why? Somewhere to read about this?