user507220
user507220

Reputation:

typical way of running clojure programs

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.

Upvotes: 21

Views: 9465

Answers (7)

Tyler
Tyler

Reputation: 581

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.

Upvotes: 0

CrashOverride
CrashOverride

Reputation: 669

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

Upvotes: 1

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91534

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).

Upvotes: 28

djhaskin987
djhaskin987

Reputation: 10057

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.

Upvotes: 8

Paul Sanwald
Paul Sanwald

Reputation: 11329

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.

Upvotes: 9

Shantanu Kumar
Shantanu Kumar

Reputation: 1240

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

Upvotes: 6

number23_cn
number23_cn

Reputation: 4619

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

Upvotes: 2

Related Questions