How can I run a .clj Clojure file I created?

2019-03-08 07:35发布

I've installed Geany on my Linux Mint machine and I wrote some simple code. Just a small hello world to get started with the language.

Now I'd like to run it and see what it outputs.

How can I run this code? I'm not using an IDE or something that's pre packaged, just the simple text editor Geany.

What command should I run?

8条回答
Deceive 欺骗
2楼-- · 2019-03-08 07:59

For running a single file through command line use the following commands:

clj -m (NameSpace name)

ex: clj -m Sample

Or if you want to run through repl:

(load-file filelocation)

ex:

 (load-file "Sample.clj")
查看更多
来,给爷笑一个
3楼-- · 2019-03-08 08:00

if you just want to execute a single file, how about piping it to the repl like:

cat $PATH_TO_FILE | lein repl

查看更多
对你真心纯属浪费
4楼-- · 2019-03-08 08:01

I had similar issue in running a specific clojure script file out of a clojure project using lein. Then I found a shortcut, thought of sharing the same.

In project.clj file, you can toggle the clojure script file name ( which has main method of course). Following is the statement for this.

:main ^:skip-aot conv.newconv ; here conv is namespace and newconv is the file name .

Note: I am new to clojure and I am not sure why '^:skip-aot` is used, I have not checked that.

Note: This methodology, does not require any plugin to be installed nor any jar file need to be generated. This only requires to change a name in project file.

Assumption: Your clojure project has multiple clojure files, each having main method. I am assuming this is created for testing purpose only. My solution will work for this specific scenario.

查看更多
Viruses.
5楼-- · 2019-03-08 08:10

Once you've installed lein and the lein-exec plugin, running the .clj file you've created is as simple as

lein exec hello.clj

In case you're passing command line arguments like

lein exec hello.clj arg1 arg2 arg3

you can access them in the 'foo' function in your hello.clj like

(foo *command-line-args*) 
查看更多
Evening l夕情丶
6楼-- · 2019-03-08 08:14

For a single clj file you can add,

#!/usr/bin/env java -cp /path/to/clojure-1.2.0.jar clojure.main

to the top of the file and make it executable or you can use leiningen which is a clojure build tool it will create a single jar that has everything packed, then you can just do,

java -jar cool_app.jar
查看更多
Root(大扎)
7楼-- · 2019-03-08 08:20

With clojure

I can't believe no one has suggested this yet. You should be able to just do

clojure path/to/script.clj

This works using the clojure installed on Ubuntu with apt-get. Not sure about other installs...

With lein-exec

However, if the script you're working on has any dependencies, I would recommend the lein-exec plugin. This has the advantage of letting you use Leiningen to handle any dependencies, which is sweet. Leiningen already has a lein run command, but it only works for executing main functions within a full Clojure/lein project. So the lein-exec plugin is a really nice complement for small scale scripting.

Dependency Details...

Note: For the sake of thoroughness, if using lein, you can add any libraries you would like to use to a ~/.lein/profiles.clj file, or to your project.clj file if you're working in a project. Then when you run lein deps all dependencies will be installed and accessible in your scripts/projects when using lein exec/lein run. Additionally, lein repl gives you a Clojure repl with access to those dependencies. Again, definitely the way to go if you have any library dependencies.

Either way, consider using drip for faster start time

Drip is a neat little tool that provides you with a pre-bootstrapped JVM image ready to launch. This can be nice for cutting down JVM application launch times. Clojure can take quite a while to bootstrap itself, so drip is a nice tool for helping to speed up that process. This is especially the case when you're writing little scripts, with which there is typically an expectation of running quickly. If you're using leiningen, take a look at the lein-drip plugin.

For ClojureScript

I'd recommend http://planck-repl.org/. It now supports (bootstrapped) quick launching of ClojureScript scripts without having to fire up the JVM or Clojure. For most scripting tasks, quick execution is important, so when you don't need anything specific to the JVM, this is my #1 recommendation.

查看更多
登录 后发表回答