I'm trying to compile clojure source into class file, and run it using command line only, no lein, nor (possibly) reply.
I have core.clj in src/hello
directory.
.
└── src
└── hello
└── core.clj
This is source code.
(ns hello.core)
(defn -main
"This should be pretty simple."
[]
(println "Hello, World!"))
(compile)
in REPL. From the hint in this site ( http://clojure.org/compilation ), I tried to get class file from REPL.
I started REPL with lein repl
in src directory, then tried to compile to get an error.
user=> (compile 'hello.core)
CompilerException java.io.IOException: No such file or directory, compiling:(hello/core.clj:1:1)
From this post simple tool for compiling Clojure .clj into .class / .jar and How to compile file in clojure , it seems like that I can compile the clojure source outside REPL.
I tried this in .
to get an error.
> 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)
...
So, here are my questions:
java -cp .:CLOJURE_JAR main
? When invoking REPL, you should add class path.
java -cp .:<PATH>/clojure-1.6.0.jar:./src clojure.main
You need to set the *compile-path*
with (set! *compile-path* "build")
.
Then you can compile to get the class file.
user=> (compile 'hello.core)
hello.core
There are two issues:
This is to invoke the compiler.
clojure> java -cp .:<PATH>/clojure-1.6.0.jar:./src -Dclojure.compile.path=build clojure.lang.Compile hello.core
Compiling hello.core to build
You should point to the directory where the class files are located.
clojure> java -cp .:<PATH>/clojure-1.6.0.jar:./build hello.core
Hello, World!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.