graalvm

conao3 2024-07-20T17:35:53.557189Z

(defn- home-path []
  (System/getProperty "user.home"))

(defn- local-dir []
  (io/file (home-path) ".local"))

(defn -main [& _args]
  (println (seq (.list (io/file (home-path) ".local"))))
  (println (seq (.list (local-dir)))))
1st one is working as expected but 2nd one is not working only native compiled using GraalVM.
$ make build.native

$ clj -M -m graalvm-list-issue.main
(bin opt lib work state poetry python include pipx npm libexec tools share head)
(bin opt lib work state poetry python include pipx npm libexec tools share head)

$ java -jar target/graalvm-list-issue-standalone.jar
(bin opt lib work state poetry python include pipx npm libexec tools share head)
(bin opt lib work state poetry python include pipx npm libexec tools share head)

$ ./target/graalvm-list-issue
(bin opt lib work state poetry python include pipx npm libexec tools share head)
Exception in thread "main" java.lang.IllegalArgumentException: No matching field found: list for class java.io.File
	at clojure.lang.Reflector.getInstanceField(Reflector.java:414)
	at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:457)
	at graalvm_list_issue.main$_main.invokeStatic(main.clj:14)
	at graalvm_list_issue.main$_main.doInvoke(main.clj:12)
	at clojure.lang.RestFn.invoke(RestFn.java:397)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.RestFn.applyTo(RestFn.java:132)
	at graalvm_list_issue.main.main(Unknown Source)
	at java.base@22.0.1/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
Any suggestions? Full code is here: https://github.com/conao3/clojure-graalvm-list-issue

Bob B 2024-07-20T17:42:25.562309Z

probably applicable: <https://github.com/clj-easy/graal-docs?tab=readme-ov-file#reflection>

2024-07-20T17:42:35.401219Z

make sure you use (set! *warn-on-reflection* true) at the top of all your files when using graalvm native-image for your file:

Reflection warning, graalvm_list_issue/main.clj:18:17 - reference to field list can't be resolved.
you could do
(defn- local-dir ^java.io.File []
  (io/file (home-path) ".local"))
or add the reflection to your native image config

➕ 1
conao3 2024-07-20T17:45:55.776139Z

wow, thanks! Confirmed. Thanks for very quick answer @highpressurecarsalesm and @skynet!

borkdude 2024-07-20T18:07:23.142619Z

👍