从命令行(不使用 lein)将 clojure 源代码编译成 class (AOT)
Compile clojure source into class (AOT) from command line (not using lein)
我正在尝试将 clojure 源代码编译成 class 文件,运行 它只使用命令行,没有 lein,也没有(可能)回复。
我在 src/hello
目录中有 core.clj。
.
└── src
└── hello
└── core.clj
这是源代码。
(ns hello.core)
(defn -main
"This should be pretty simple."
[]
(println "Hello, World!"))
在 REPL 中使用 (compile)
。
根据本网站 (http://clojure.org/compilation) 的提示,我尝试从 REPL 获取 class 文件。
我在 src 目录下用 lein repl
启动 REPL,然后尝试编译得到一个错误。
user=> (compile 'hello.core)
CompilerException java.io.IOException: No such file or directory, compiling:(hello/core.clj:1:1)
命令行
由此post simple tool for compiling Clojure .clj into .class / .jar and How to compile file in clojure,看来我可以在 REPL 之外编译 clojure 源代码。
我在 .
中尝试过此操作,但出现错误。
> java -cp .:<PATH>/clojure-1.6.0.jar -Dclojure.compile.path=build clojure.lang.Compile src/hello/core.clj
Compiling src/hello/core.clj to build
Exception in thread "main" java.io.FileNotFoundException: Could not locate
hello/core/clj__init.class or hello/core/clj.clj on classpath:
at clojure.lang.RT.load(RT.java:443)
at clojure.lang.RT.load(RT.java:411)
...
所以,这是我的问题:
- 如何编译 clojure 源代码以获得 class with/without REPL?
- 如何运行 class 与 Java?执行
java -cp .:CLOJURE_JAR main
就够了吗?
REPL
调用REPL时,需要添加class路径。
java -cp .:<PATH>/clojure-1.6.0.jar:./src clojure.main
您需要将 *compile-path*
设置为 (set! *compile-path* "build")
。
然后就可以编译得到class文件了。
user=> (compile 'hello.core)
hello.core
命令行
有两个问题:
- 对于编译,CP 应包含源目录。
- 参数不是文件路径,而是命名空间。
这是调用编译器。
clojure> java -cp .:<PATH>/clojure-1.6.0.jar:./src -Dclojure.compile.path=build clojure.lang.Compile hello.core
Compiling hello.core to build
执行 class 文件
您应该指向 class 文件所在的目录。
clojure> java -cp .:<PATH>/clojure-1.6.0.jar:./build hello.core
Hello, World!
参考资料
- Compiling Clojure?
- http://clojure.org/compilation
我正在尝试将 clojure 源代码编译成 class 文件,运行 它只使用命令行,没有 lein,也没有(可能)回复。
我在 src/hello
目录中有 core.clj。
.
└── src
└── hello
└── core.clj
这是源代码。
(ns hello.core)
(defn -main
"This should be pretty simple."
[]
(println "Hello, World!"))
在 REPL 中使用 (compile)
。
根据本网站 (http://clojure.org/compilation) 的提示,我尝试从 REPL 获取 class 文件。
我在 src 目录下用 lein repl
启动 REPL,然后尝试编译得到一个错误。
user=> (compile 'hello.core)
CompilerException java.io.IOException: No such file or directory, compiling:(hello/core.clj:1:1)
命令行
由此post simple tool for compiling Clojure .clj into .class / .jar and How to compile file in clojure,看来我可以在 REPL 之外编译 clojure 源代码。
我在 .
中尝试过此操作,但出现错误。
> java -cp .:<PATH>/clojure-1.6.0.jar -Dclojure.compile.path=build clojure.lang.Compile src/hello/core.clj
Compiling src/hello/core.clj to build
Exception in thread "main" java.io.FileNotFoundException: Could not locate
hello/core/clj__init.class or hello/core/clj.clj on classpath:
at clojure.lang.RT.load(RT.java:443)
at clojure.lang.RT.load(RT.java:411)
...
所以,这是我的问题:
- 如何编译 clojure 源代码以获得 class with/without REPL?
- 如何运行 class 与 Java?执行
java -cp .:CLOJURE_JAR main
就够了吗?
REPL
调用REPL时,需要添加class路径。
java -cp .:<PATH>/clojure-1.6.0.jar:./src clojure.main
您需要将 *compile-path*
设置为 (set! *compile-path* "build")
。
然后就可以编译得到class文件了。
user=> (compile 'hello.core)
hello.core
命令行
有两个问题:
- 对于编译,CP 应包含源目录。
- 参数不是文件路径,而是命名空间。
这是调用编译器。
clojure> java -cp .:<PATH>/clojure-1.6.0.jar:./src -Dclojure.compile.path=build clojure.lang.Compile hello.core
Compiling hello.core to build
执行 class 文件
您应该指向 class 文件所在的目录。
clojure> java -cp .:<PATH>/clojure-1.6.0.jar:./build hello.core
Hello, World!
参考资料
- Compiling Clojure?
- http://clojure.org/compilation