简体   繁体   中英

How to run a Clojure File?

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

来自Leiningen的基本用法

$ lein run -m my.namespace # run the -main function of a namespace

您应该阅读Leiningen自述文件教程 ,或者按照您喜欢的编辑器的一些简单教程。

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM