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!
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.)
$ lein run -m my.namespace # run the -main function of a namespace
You should read up Leiningen readme and tutorial, and perhaps follow some simple tutorial for your favorite editor.
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!
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.
Exception in thread "main" java.io.FileNotFoundException: <FILE>.clj (No such file or directory)
, my file is packaged in the jar. –
Noles java -cp <JAR> clojure.main -m <NAMESPACE> <ARG1> <ARG2>
–
Noles © 2022 - 2024 — McMap. All rights reserved.
java
withoutlein
: #30151887 – Doro