Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not idiomatic to run Clojure scripts over and over from the command line. The REPL is a better command line. Clojure being a Lisp, it's common to fire up Clojure and leave the same instance running forever, and interact with it rather than restart it. You can change functions in the running instance one at a time, run them and poke them as needed. Escaping the tedious and slow traditional edit/compile/debug cycle is a great feature of Lisps. </p> <p>You can easily write functions to do things like run unit tests, and just call those functions from the REPL whenever you want to run them and ignore them otherwise. It's common in Clojure to use <code>clojure.contrib.test-is</code>, add your test functions to your namespace, then use <code>clojure.contrib.test-is/run-tests</code> to run them all.</p> <p>Another good reason not to run Clojure from the commandline is that the startup time of the JVM can be prohibitive.</p> <p>If you really want to run a Clojure script from the command line, there are a bunch of ways you can do it. See <a href="http://groups.google.com/group/clojure/browse_thread/thread/ca60d98fb4f7e71e/ddc68367d4fa1bc7" rel="noreferrer">the Clojure mailing list</a> for some discussion.</p> <p>One way is to test for the presence of command line arguments. Given this <code>foo.clj</code> in the current directory:</p> <pre><code>(ns foo) (defn hello [x] (println "Hello," x)) (if *command-line-args* (hello "command line") (hello "REPL")) </code></pre> <p>You'll get different behavior depending how you start Clojure.</p> <pre><code>$ java -cp ~/path/to/clojure.jar:. clojure.main foo.clj -- Hello, command line $ java -cp ~/path/to/clojure.jar:. clojure.main Clojure 1.1.0-alpha-SNAPSHOT user=&gt; (use 'foo) Hello, REPL nil user=&gt; </code></pre> <p>See <code>src/clj/clojure/main.clj</code> in the Clojure source if you want to see how this is working.</p> <p>Another way is to compile your code into <code>.class</code> files and invoke them from the Java command line. Given a source file <code>foo.clj</code>:</p> <pre><code>(ns foo (:gen-class)) (defn hello [x] (println "Hello," x)) (defn -main [] (hello "command line")) </code></pre> <p>Make a directory to store the compiled <code>.class</code> files; this defaults to <code>./classes</code>. You must make this folder yourself, Clojure won't create it. Also make sure you set <code>$CLASSPATH</code> to include <code>./classes</code> and the directory with your source code; I'll assume <code>foo.clj</code> is in the current directory. So from the command line:</p> <pre><code>$ mkdir classes $ java -cp ~/path/to/clojure.jar:./classes:. clojure.main Clojure 1.1.0-alpha-SNAPSHOT user=&gt; (compile 'foo) foo </code></pre> <p>In the <code>classes</code> directory you will now have a bunch of <code>.class</code> files. To invoke your code from the command line (running the <code>-main</code> function by default):</p> <pre><code>$ java -cp ~/path/to/clojure.jar:./classes foo Hello, command line. </code></pre> <p>There's a lot of information about compiling Clojure code on <a href="http://clojure.org/compilation" rel="noreferrer">clojure.org</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload