21

I'm new to Clojure and really confused about how I should run Clojure programs.

My first question is whether every Clojure program is a Leiningen package? If I want to write do I start by creating a new Leiningen project? Is there a to run Clojure programs similar to Python(ie python pyfile.py)

I realize this might be a stupid question, but I've been confused about this for a while and the few books/tutorials I've gone through don't seem to answer this question properly. Upto now, I've been running Clojure code just in the REPL.

7 Answers 7

28

Since you found leiningen straight off you're on the right track. It's not a silly question because answering it will help others get better google results.

  • each clojure program should be a separate leiningen project, so you run lein new project-name for each of them.

  • the result of building a Clojure project is the same as a Java project, a JAR file that gets run with java -jar myproject-standalone.jar for instance

  • leiningen can build a wrapper shell script for you that produces a .sh file that just calls java -jar

  • When I run clojure programs in production at work we just check them out of git and then call lein run in the directory (actually Jenkins does this).

2
9

One thing to add to Arthur Ulfeldt's excellent answer is that lein creates a project.clj for you, and your main function (function where execution of the program starts from) can be defined in there. By default Leiningen creates the main function in a file called core.clj For example:

(defproject music "1.0.0-SNAPSHOT"
  :description "A workspace for my music experiments"
  :dependencies [
    [org.clojure/clojure "1.3.0"]
    [org.clojure/math.combinatorics "0.0.1"]
  ]
  :main music.core)

now my project can be run using:

lein run

and the main function in music.core will be run.

0
8

To answer part of the question: "Is there a to run Clojure programs similar to Python?" You really can just run a clojure script like this (called hello.clj):

(println "Hello!")

like this:

clojure hello.clj

EDIT: The following should work on all systems:

java -jar <path-to-clojure-jar>/clojure.jar hello.clj

On my system, the clojure.jar file is found in /usr/share/clojure/clojure.jar . The linux command to fire off clojure simply runs a command like the one above under the covers.

1
  • Does this work on all systems? I know that installing from aptitude on Ubuntu gives you a clojure command, but I don't know that this is true for other installs. Would be good to verify and update here. Oct 16, 2013 at 5:17
6

Probably the easiest way would be to use lein-exec to run Clojure scripts:

http://charsequence.blogspot.in/2012/04/scripting-clojure-with-leiningen-2.html

1
  • 1
    This really adds to the answers in this question, as part of the question is "Is there a way to run Clojure programs similar to Python?" This answer shows us how. Sometimes, I just want to run a script. Sep 6, 2013 at 2:47
2

my bash clojure script:

#!/bin/sh

export CLOJURE_EXT=$HOME/.clojure
export CLOJURE_OPTS="-Xmx1g -server"

LIBS=$(ls -1 $CLOJURE_EXT/* 2> /dev/null)
export CLASSPATH=.:$CLOJURE_EXT:$CLASSPATH:"$(echo "$LIBS" | tr \\n :)"

JAVA=${CLOJURE_JAVA:-java}
OPTS=${CLOJURE_OPTS:-}
MAIN=${CLOJURE_MAIN:-clojure.main}

breakchars="(){}[],^%$#@\"\";:''|\\"

if [ $# -eq 0 ]; then
    exec rlwrap --remember -c -b $breakchars \
        -f $HOME/.clj_completions \
        --history-filename=$HOME/.clj_history --histsize=1000000 \
        $JAVA $OPTS $MAIN -i $HOME/.clojure.clj --repl
else
    exec $JAVA $OPTS $MAIN "$@"
fi

save them to $HOME/bin/clojure, chmod +x $HOME/bin/clojure, copy clojure.jar and the 3rd party library into $HOME/.clojure, you can run clojure your_program.clj as python pyfile.py

1

As an update, a simple way to deal with clojure programs is to use leiningen

If you want to run small clojure scripts, use lein-exec

0

Create an alias in your .bashrc file, or equivalent:

alias clojure="java -jar path/to/clojure-x.x.x.jar"

Then you can run clojure my-file.clj in your terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.