[英]clojure.java.io ClassNotFoundException
我正在嘗試在我的 Mac 上運行以下程序:
(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’
我怎樣才能把go包括clojure.java.io class?
以下是您通常如何在源代碼文件中編寫它:
(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...)
function 調用不同的語法和引用。
如果您將這些行輸入到 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 備忘單!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.