clojure.java.io ClassNotFoundException
我正试图在Mac上运行以下程序:clojure.java.io ClassNotFoundException,clojure,Clojure,我正试图在Mac上运行以下程序: (ns clojure.examples.hello (:gen-class)) (require ‘clojure.java.io’) (defn Example [] (.exists (file "Example.txt"))) (Example) 我使用以下命令执行此操作: clojure Exists.clj 但这给了我以下错误: Syntax error (ClassNotFoundException) compi
(ns clojure.examples.hello
(:gen-class))
(require ‘clojure.java.io’)
(defn Example []
(.exists (file "Example.txt")))
(Example)
我使用以下命令执行此操作:
clojure Exists.clj
但这给了我以下错误:
Syntax error (ClassNotFoundException) compiling at (Exists.clj:5:1).
‘clojure.java.io’
如何将clojure.java.io类包括在内?以下是您通常在源代码文件中编写的方法:
(ns tst.demo.core
(:require [clojure.java.io :as io]) ; proper form, but not used anywhere
(:import [java.io File]))
(println (spit "demo.txt" "stuff happens"))
(println (slurp "demo.txt"))
(println (.exists (java.io.File. "./demo.txt"))) ; will work w/o `:import` above
(println (.exists (File. "./demo.txt"))) ; requires `:import` declaration above
结果如下:
(spit "demo.txt" "stuff happens") => nil
(slurp "demo.txt") => "stuff happens"
(.exists (java.io.File. "./demo.txt")) => true
(.exists (File. "./demo.txt")) => true
请注意,在ns
表单中使用:require
关键字需要不同于使用(require…
函数调用的语法和引号
如果要在REPL中键入这些行,可以执行以下操作:
demo.core=> (ns demo.core)
nil
demo.core=> (require '[clojure.java.io :as io]) ; function-call version
nil
demo.core=> (spit "demo.txt" "stuff happens")
nil
demo.core=> (println (slurp "demo.txt"))
stuff happens
nil
您可能会发现这对您的入门很有帮助。还要确保查看文档来源列表,尤其是Clojure备忘单 clojure.java.io附近的引号太多了。使用撇号引用Lisp符号时,仅使用一个撇号作为符号前缀。