How to run a Clojure File?
Asked Answered
H

5

11

I am an absolute beginner in clojure. I have clojure1.6 and lein (i use ubuntu 15.04). how can i run the clojure file i have created using the terminal? Is there some command like clojure file.clj for this? I appreciate your help!

Halfsole answered 25/5, 2015 at 20:27 Comment(1)
this might be helpful too: here's how to run a simple clojure program using just java without lein: #30151887Doro
L
13

The simple answer:

If 'clojure' is a script or alias that will run Clojure (by running java with appropriate command line arguments), and "foo.clj" is the name of your Clojure source file, then you can just enter

clojure foo.clj

If this doesn't work, then it's because no such script is in your path, but you can make a script whose contents look something like this:

#!/bin/sh
java -cp /usr/lib/clojure-1.6.0.jar clojure.main "$@" -r

You'll need to figure out where the Clojure jar file is, and replace the piece after -cp. With Leiningen, you probably have some version of this file under ~/.m2.

If you start up Clojure, and get a REPL prompt, then you can enter:

(load-file "foo.clj")

or

(load-file "<path to foo.clj>/foo.clj")

The good answer:

See Shlomi and Daniel Compton's answers.

When you're starting out with Clojure, there can be little bit of a learning curve concerning how to set up your directory structure and filenames to work easily with Leiningen and Clojure. (If you know Java well, this is a little bit easier.) However, once you become familiar with Leiningen's project.clj file and how to set up your source files and namespaces (after you learn about namespaces!), doing things in the conventional way will make your life with Clojure much easier than if you just keep using -m and load-file. (Clojure is really better designed for developing small or large projects, rather than running one-off scripts. Clojure takes too long to start up for use as a scripting language, and with Leiningen, it takes even longer.)

Limewater answered 26/5, 2015 at 0:11 Comment(0)
G
9

From Leiningen basic-usage

$ lein run -m my.namespace # run the -main function of a namespace
Gourley answered 25/5, 2015 at 21:20 Comment(0)
S
6

You should read up Leiningen readme and tutorial, and perhaps follow some simple tutorial for your favorite editor.

Sidle answered 25/5, 2015 at 20:43 Comment(0)
K
4

First of all, Clojure programmers tend to use the REPL more often than running stand-alone files. But if you do want to run files, I'd suggest Boot for quick hacks.

Installation instructions for Unix are on the Github Boot project page. Once you have Boot installed, create a file called main.boot containing:

#!/usr/bin/env boot

(defn -main [& args]
  (println "Hello world!")
  (System/exit 0))

Then make it executable and run it:

$> chmod a+x main.boot
$> ./main.boot
Hello world!

Here's a pretty good introduction to Boot.

Karlkarla answered 26/5, 2015 at 16:21 Comment(0)
S
0
java -cp clojure-1.7.0.jar clojure.main -i file.clj

assuming that java is installed, and that both the clojure-1.7.0.jar file and file.clj are in the current directory

If you end up needing other jar files for libraries etc, then this eventually becomes rather overwhelming. At that point you might want to investigate boot, maven, or leiningen. Most people seem to use leiningen, but it sets my hair on fire.

Satellite answered 1/3, 2019 at 16:8 Comment(2)
Do you mean file.clj should exist outside of the jar? I keep getting Exception in thread "main" java.io.FileNotFoundException: <FILE>.clj (No such file or directory), my file is packaged in the jar.Noles
Thanks for the suggestion. I however was able to succeed with a single file (packaged in jar) by running this command from the root of classpath : java -cp <JAR> clojure.main -m <NAMESPACE> <ARG1> <ARG2>Noles

© 2022 - 2024 — McMap. All rights reserved.