简体   繁体   English

编译Clojure?

[英]Compiling Clojure?

I'm feeling slightly silly here, but I can't get Clojure Hello World to compile.我在这里感觉有点傻,但我无法编译 Clojure Hello World。

Directory structure:目录结构:

hello-world/
  clojure-1.1.0.jar
  build/
    classes/
  src/
    test/
      hello.clj

hello.clj:你好.clj:

(ns test.hello
  (:gen-class))

(defn -main [& args]
  (println "Hello" (nth args 0)))

Interaction:相互作用:

$ cd hello-world
[hello-world]$ java -cp ./clojure-1.1.0.jar:./build/classes:./src clojure.main
Clojure 1.1.0
user=> (require 'test.hello)
nil
user=> (test.hello/-main "there")
Hello there
nil
user=> (compile 'test.hello)
java.io.IOException: No such file or directory (hello.clj:2)
user=> *compile-path*
"classes"
user=> (doseq [p (.split (System/getProperty "java.class.path") ":")] (println p))
./clojure-1.1.0.jar
./build/classes
./src
nil

So I can load and call the file from the REPL, but it doesn't compile.所以我可以从 REPL 加载和调用文件,但它不能编译。

According to clojure.org , compilation needs根据clojure.org ,编译需要

  • namespace must match classpath-relative file path - check命名空间必须匹配类路径相对文件路径 - 检查
  • *compile-path* must be on the classpath - check *compile-path* 必须在类路径上 - 检查
  • :gen-class argument to the ns form - check ns 形式的 :gen-class 参数 - 检查

I found this post from a year back , as far as I can tell I'm doing exactly the same, but it doesn't work.一年前发现了这篇文章,据我所知,我正在做完全相同的事情,但它不起作用。

What am I missing?我错过了什么?

System: OS X 10.6, Java 1.6.0, Clojure 1.1系统:OS X 10.6、Java 1.6.0、Clojure 1.1

Got it, there's a fourth requirement:明白了,还有第四个要求:

  • *compile-path* is resolved relative to the JVMs working directory, normally the directory where java is started. *compile-path* 是相对于 JVM 工作目录解析的,通常是启动 java 的目录。 Or by REPL: (System/getProperty "user.dir") ,或者通过 REPL: (System/getProperty "user.dir")

So this works:所以这有效:

user=> (set! *compile-path* "build/classes")     
"build/classes"
user=> (compile 'test.hello)
test.hello

Why you don't use Leiningen ?为什么不使用莱宁根 It's much easier to use it, than compile code manually.使用它比手动编译代码要容易得多。 You can use my article about it as introduction...您可以使用关于它的文章作为介绍...

运行clojure文件

clojure filename.clj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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