Sunday, September 8, 2013

How to make a Clojure program executable (Mac or Linux)

1. Create the project

Using Eclipse (and CounterClockwise) create a new project to try this out.
Create the project using the Leiningen option, the default.

For this example I created a project named, getting-data

2. Edit the source file (:gen-class)

In the getting-data.core file, in the src folder:

Add the directive:

I have also added a simple "say hello" function.

(ns getting-data.core

(defn say-hello [n]
  (println "hello, " n))

(defn -main [& args]
 (say-hello "john"))

3. Edit the project.clj file - :main getting-data.core

(defproject getting-data "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "Eclipse Public License"
            :url ""}
  :dependencies [[org.clojure/clojure "1.5.1"]]
  :main getting-data.core)

You can see the text  :main getting-data.core has been added.

4. Download Leiningen.

This needs to be done to run the executable from the command line. Counterclockwise instals its own version of Lein but we will need our own.
This is very simple.
Go to and download the script. You place the script where it can be executed, make it executable and run it. This will install Leiningen.

These are the instructions from the Leiningen page:
  1. Make sure you have a Java JDK version 6 or later.
  2. Download the script.
  3. Place it on your $PATH. (~/bin is a good choice if it is on your path.)
  4. Set it to be executable. (chmod 755 ~/bin/lein)
Downloading on the Mac will place the file into the Download folder. use the mv unix comand to move it and rename the file if needs be. You may need to remove a ".txt" extension of the file as you mv it to the ~/bin folder.

5. Navigate to the project in Terminal

This may be similar to this: cd ~/Documents/workspace/getting-data/

Check the files exist:

John-Mac-mini:getting-data John$ ls -l
total 16
-rw-r--r--  1 John  staff  202  7 Sep 19:56
drwxr-xr-x  3 John  staff  102  7 Sep 19:56 bin
drwxr-xr-x  3 John  staff  102  7 Sep 19:56 doc
-rw-r--r--  1 John  staff  298  8 Sep 13:56 project.clj
drwxr-xr-x  2 John  staff   68  7 Sep 19:56 resources
drwxr-xr-x  3 John  staff  102  7 Sep 19:56 src
drwxr-xr-x  3 John  staff  102  7 Sep 19:56 target
drwxr-xr-x  3 John  staff  102  7 Sep 19:56 test

Now just check the project file is correct:

Enter cat project.clj at the command line.

John-Mac-mini:getting-data John$ cat project.clj
(defproject getting-data "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "Eclipse Public License"
            :url ""}
  :dependencies [[org.clojure/clojure "1.5.1"]]
  :main getting-data.core)

Yes that looks like the file.

6. Now run the file using, lein run

John-Mac-mini:getting-data John$ lein run
hello,  john

This doesn't create the executable, it simply runs the project. We can leave this step out if we wish.

7. Now we can create the executable Jar

Use, lein uberjar

This will contain all the dependencies and will be placed in the ./target folder.

John-Mac-mini:getting-data John$ lein uberjar
Warning: specified :main without including it in :aot. 
Implicit AOT of :main will be removed in Leiningen 3.0.0. 
If you only need AOT for your uberjar, consider adding :aot :all into your
:uberjar profile instead.
Compiling getting-data.core
Created /Users/John/Documents/workspace/getting-data/target/getting-data-0.1.0-SNAPSHOT.jar
Created /Users/John/Documents/workspace/getting-data/target/getting-data-0.1.0-SNAPSHOT-standalone.jar

It has created two files:

8. Run the file

java -jar ./target/getting-data-0.1.0-SNAPSHOT-standalone.jar 

No comments:

Post a Comment