clojure

How to interoperate from clojure with java varargs methods


According with docs I have to use into-array but it doesn't work.

(java.nio.file.Paths/get (into-array ["myfolder" "aaa.txt"]))
; Execution error (ClassCastException) at msm.config/eval10595 (form-init12148349852569231555.clj:153).
; class [Ljava.lang.String; cannot be cast to class java.net.URI ([Ljava.lang.String; and java.net.URI are in module java.base of loader 'bootstrap')

So, I am not completely understand how to do it...


Solution

  • Paths/get does not take just an array. It takes a first string argument before the array. This signals, that at least one argument is needed. And while this looks nice when used in Java, it is a little meh on the JVM side.

    (java.nio.file.Paths/get "/tmp" (into-array ["a" "b" "c"]))
    ; #object[sun.nio.fs.UnixPath 0x3e792ce3 "/tmp/a/b/c"]