简体   繁体   中英

How do I require clojure.java.io with lein exec?

I am totally new to clojure. I would like to start writing simple scripts, and I came across lein-exec as a means of doing so even if a script contains dependencies. Although I can run an example I found online, I don't know how to require clojure.java.io.

(require 'leiningen.exec)

;places the dependency on the classpath
(leiningen.exec/deps '[[enlive/enlive "1.1.4"]])
(require '[net.cgrand.enlive-html :as html])

How would I require something like clojure.java.io using lein exec?

EDIT: adding more detail

(require 'leiningen.exec)

(leiningen.exec/deps '[[clojure.java.io]])

(require 'clojure.java.io)

(defn Example []
   (.exists (clojure.java.io "Example.txt")))

(Example)

gives me

Caused by: java.lang.IllegalArgumentException: Provided artifact is missing a version: [clojure.java.io]

and this

(require 'leiningen.exec)

;(leiningen.exec/deps '[[clojure.java.io]])

(require 'clojure.java.io)

(defn Example []
   (.exists (clojure.java.io "Example.txt")))

(Example)

gives me

Caused by: java.lang.ClassNotFoundException: clojure.java.io

Well, personally, I'd recommend using inlein instead of lein-exec for doing scripting with Clojure:

I find it nicer, and it uses standard lein dependency map for defining your dependencies.

Secondly, this: (require 'clojure.java.io) IS the right way to require clojure.java.io . That namespace is included with Clojure itself, so you don't need to declare an additional dependency on any other library to use it.

Your problem is that you are not using it correctly:

(.exists (clojure.java.io "Example.txt"))

In the above code, you are calling clojure.java.io as if it was a function, but it is not a function, it is a namespace. You need to choose a function inside of it to call, such as the file function. If this was java, you could kinda think of the namespace as the class, and the function as a method on the class. If you do:

(.exists (clojure.java.io/file "Example.txt"))

It should work now. Notice how in Clojure, the syntax is: namespace/function . This is different the some other languages like Java or Python, where you would have: some.location.class.method instead, basically the function is also separated by a . . This is not the case in Clojure, in Clojure, the function part is separated by a / .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM