[英]clojure.java.io ClassNotFoundException
I am trying to run the following program on my Mac:我正在尝试在我的 Mac 上运行以下程序:
(ns clojure.examples.hello
(:gen-class))
(require ‘clojure.java.io’)
(defn Example []
(.exists (file "Example.txt")))
(Example)
I do this wih the following command:我使用以下命令执行此操作:
clojure Exists.clj
But this gives me the following error:但这给了我以下错误:
Syntax error (ClassNotFoundException) compiling at (Exists.clj:5:1).
‘clojure.java.io’
How can I go about including the clojure.java.io class?我怎样才能把go包括clojure.java.io class?
Here is how you would normally write this in a source code file:以下是您通常如何在源代码文件中编写它:
(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
with results:结果:
(spit "demo.txt" "stuff happens") => nil
(slurp "demo.txt") => "stuff happens"
(.exists (java.io.File. "./demo.txt")) => true
(.exists (File. "./demo.txt")) => true
Note that using the :require
keyword in a ns
form requires different syntax and quoting than using the (require...)
function call.请注意,在
ns
表单中使用:require
关键字需要与使用(require...)
function 调用不同的语法和引用。
If you are typing these lines into a REPL, you may do something like:如果您将这些行输入到 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
You may find this template project helpful in getting started.您可能会发现此模板项目有助于入门。 Also be sure to check out the list of documentation sources, esp.
还要确保查看文档来源列表,尤其是。 the Clojure CheatSheet!
Clojure 备忘单!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.