I was trying to figure out how to AOT compile a Clojure program, in order to really see some fast execution times. The simplest way to describe AOT compilation would be how its done in Java,
The invocation of the Java compiler (javac) is the pre-compilation of the source file, which is then loaded by the JVM in the next step. In the case of Clojure, when a program is run using,
The code is first compiled, and then executed – resulting in large amounts of time for the output to be displayed even for simple programs.
The AOT compile process turned out to be trickier to set up than I expected, so I thought I’d put it out there for all those who get stuck after reading the original documentation at http://clojure.org/compilation.
Firstly, the directory structure for my experiment looks like this,
I created dir1, and its subdirectories as below. The code is in clojure/examples/ and the classes/ directory is the default compile.path – something that the documentation neglected to mention. Without this path, compilation WILL fail.
The .clojure file
This file is used with my clj script, that can be obtained from here.
It contains a list of directories that are to be specified to the Clojure compiler at compile time. The file looks like,
This code of course is the default from the Clojure website, as a test.
(println (str "Hello " greetee "!")))
The next step is to invoke the Clojure REPL using the clj script, from the dir1 directory.
Type in the following to compile the program in the clojure.examples namespace,
user=> (compile 'clojure.examples.hello)
Success! And the resulting output of the classes directory is,
$ ls /dir1/classes/clojure/examples
And lastly, to run this program as any other Java program, you can use,
java -cp ./classes:/opt/jars/clojure.jar:/opt/jars/clojure-contrib.jar clojure.examples.hello Viksit
Also, I highly recommend Stuart Holloway’s book “Programming Clojure”. Its turning out to be an excellent read.