13

since a Path class does not have public constructor ,so path object are created using get factory method in Paths object.

e.g

Path p2 = Paths.get("/home/admin","Migrations","/blog/tables/6-rating.xml");

//or

Path p2 = Paths.get(new URI("file://home/debianaut/Migrations/blog.sakhunzai/tables/6-rating.xml"));

how we can do this in clojure way ?

sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • `(Paths/get "/home/admin" "Migrations" "/blog/tables/6-rating.xml") CompilerException java.lang.IllegalArgumentException: No matching method: get, compiling:(NO_SOURCE_PATH:5:2) ` – sakhunzai Aug 05 '14 at 18:22
  • `(Paths/get "/home/debianaut/Migrations/blog.sakhunzai/tables/6-rating.xml") ClassCastException java.lang.String cannot be cast to java.net.URI user/eval5598 (NO_SOURCE_FILE:11)` – sakhunzai Aug 05 '14 at 18:24

1 Answers1

14
user> (java.nio.file.Paths/get "/home/justin" (into-array [".lein" "profiles.clj"]))
#<UnixPath /home/justin/.lein/profiles.clj>

varargs java methods need an array containing all remaining args as their final argument.

The first string outside the array is needed in order for the method dispatch to match the right method.

For completeness, here is an example using a URI (much more straightforward):

user> (java.nio.file.Paths/get (java.net.URI. "file:///home/justin"))
#<UnixPath /home/justin>
noisesmith
  • 20,076
  • 2
  • 41
  • 49
  • "It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process." http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html clojure does not compile to java, and it simply lacks that feature that the java compiler provides. – noisesmith Aug 06 '14 at 15:47