简体   繁体   English

如何运行Clojure文件?

[英]How to run a Clojure File?

I am an absolute beginner in clojure. 我是clojure的绝对初学者。 I have clojure1.6 and lein (i use ubuntu 15.04). 我有clojure1.6和lein(我使用ubuntu 15.04)。 how can i run the clojure file i have created using the terminal? 如何运行我使用终端创建的clojure文件? Is there some command like clojure file.clj for this? 有没有像clojure file.clj这样的命令? 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'是将运行Clojure的脚本或别名(通过使用适当的命令行参数运行java),并且“foo.clj”是您的Clojure源文件的名称,那么您只需输入

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 . 您需要确定Clojure jar文件的位置,并在-cp之后替换该块。 With Leiningen, you probably have some version of this file under ~/.m2. 使用Leiningen,你可能在〜/ .m2下有这个文件的某个版本。

If you start up Clojure, and get a REPL prompt, then you can enter: 如果你启动Clojure并获得REPL提示,那么你可以输入:

(load-file "foo.clj")

or 要么

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

The good answer: 好的答案:

See Shlomi and Daniel Compton's answers. 见Shlomi和Daniel Compton的答案。

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. 当您开始使用Clojure时,关于如何设置目录结构和文件名以便轻松使用Leiningen和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 . (如果你对Java有所了解,这有点容易。)但是,一旦你熟悉了Leiningen的project.clj文件以及如何设置源文件和命名空间(在你了解命名空间之后!),在传统方式将使您在Clojure中的生活比使用-mload-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.) (Clojure非常适合开发小型或大型项目,而不是运行一次性脚本.Clojure需要很长时间才能用作脚本语言,而Leiningen需要更长的时间。)

来自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. 首先,Clojure程序员倾向于比运行独立文件更频繁地使用REPL。 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 . Unix的安装说明在Github Boot项目页面上 Once you have Boot installed, create a file called main.boot containing: 安装Boot后,创建一个名为main.boot的文件, main.boot包含:

#!/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 . 这是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 假设安装了java,并且clojure-1.7.0.jar文件和file.clj都在当前目录中

If you end up needing other jar files for libraries etc, then this eventually becomes rather overwhelming. 如果您最终需要其他jar文件用于库等,那么这最终会变得相当压倒性。 At that point you might want to investigate boot, maven, or leiningen. 此时您可能想要研究boot,maven或leiningen。 Most people seem to use leiningen, but it sets my hair on fire. 大多数人似乎都在使用leiningen,但它让我的头发着火了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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