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

Command LineClojure

Command Line Problem Overview


I wrote a small "hello world" program on Linux to get started with the language.

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

How can I run this code? What command should I run?

Command Line Solutions


Solution 1 - Command Line

FWIW, I'm interpreting the question here as specifically asking how to run a single file, like a script, versus as a part of a compiled project. Obviously though, there's a relationship between how and why you would want to run a single file, and how you would want to build a full project, so I weigh that in some as well.

Best choice: the clj tooling

Docs are here:

https://clojure.org/guides/deps_and_cli

This is an official part of the Clojure project, which by itself is a pretty good reason to go this route. However, it has a couple of advantages over the other solutions here:

  • cleaner dependency resolution model, via deps.edn
  • also resolve dependencies via git references!
  • ~/.clojure/deps.edn seems to do a better job of cleanly providing reusable functionality via its aliass than ~/.lein/profiles.clj
  • if you need to load your code in a repl, clj won't choke on a borked src file, like lein repl does

Simply

clj your-code-file.clj

Or if you have dependencies

clj -Sdeps '{:deps {clj-time {:mvn/version "0.14.2"}}} your-code-file.clj

Works well as a shebang in scripts as well!

#!clj -deps '{:deps {clj-time {:mvn/version "0.14.2"}}}
(ns my-crazy-script!
  (:require ...))
...

I recommend installing with homebrew, which now works on both Mac and Linux. If you're on Windows I'd recommend using the WSL+brew.

The one advantage I see of lein (see below) tooling over clj generally is that lein handles a little more of the process of building and packaging your code base, when you get to that point. However, there are other ways of handing that (see pack, uberdeps or cambada), and the advantages of clj mentioned above outweigh IMHO.

Babashka

If you are trying to run a single file, as apposed to an entire project, there's a good chance your in "scripting territory". Unfortunately, the JVM takes a while to boot up, as does Clojure itself. Via the magic of GraalVM and the SCI, Babashka is able to support most of the Clojure language with blazing fast startup times. It also comes with quite a few "batteries included" for typical scripting tasks, making it a perfect complement to clj when you want fast-executing, short lived processes.

Once you install it you can simply bb you-code-file.clj, or use #!bb in your shebang. See the docs for more usage instructions:

https://babashka.org/

With lein & lein-exec

If you prefer lein tooling, or have a ~/.lein/profiles.clj file handy already and don't want to invest in learning clj/deps.edn, you can consider looking at lein-exec.

If you already have a project.clj set up with dependencies, you can of course run lein run -m <yer-namespace>[/<yer-main-fn>].

As mentioned above, the one advantage of lein over clj is that it is a complete build tool, and can make jars/uberjars for you with lein jar or lein uberjar, do deploys and such.

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. Like babashka, this makes it great for scripting. However, I find babashka both more powerful and more natural for scripting, so I'd start there unless you have a particular reason for wanting to use Cljs (more familiarity with JS/node, need of certain cljs libraries, etc).

For more substantial projects (beyond scripting), you may want to look at Shadlow-CLJS, for its ease of incorporating vanilla JS projects. And FWIW, Shadow nicely integrates with both deps and lein, and has it's own built in hode code reloading, like figwheel.

With java

You can, of course, as has been suggested in another answer, use

java -cp clojure.jar clojure.main file.clj

But you don't see that in the wild too often, because it's a pain to have to find the path to your clojure.jar and manually composing classpaths for dependencies sucks. But, you know, to each their own.

Solution 2 - Command Line

You can run script with following command:

java -cp clojure.jar clojure.main file.clj

but it's better to use leiningen, especially when you'll start to add dependencies to your project. lein provides number of commands to run your code (with all necessary dependencies), pack code into archive with lein jar, or create complete, independent archives with lein uberjar that you can run with:

java -jar your_app.jar

command

P.S. You can read how to use lein in following article - it describes base tasks & configurations

Solution 3 - Command Line

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

Solution 4 - Command Line

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

Solution 5 - Command Line

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

cat $PATH_TO_FILE | lein repl

Solution 6 - Command Line

Drip is probably now the best answer to this question (see Drip's wiki for details on using Drip with Clojure).

Cake was incorporated into Leiningen and has since been superseded as the most stable implementation of Clojure automation by Drip - see this answer to a similar question here.

Solution 7 - Command Line

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

Solution 8 - Command Line

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOnly Bolivian HereView Question on Stackoverflow
Solution 1 - Command LinemetasoarousView Answer on Stackoverflow
Solution 2 - Command LineAlex OttView Answer on Stackoverflow
Solution 3 - Command LinefiniterecursionView Answer on Stackoverflow
Solution 4 - Command LineHamza YerlikayaView Answer on Stackoverflow
Solution 5 - Command LineWill MunnView Answer on Stackoverflow
Solution 6 - Command LineduncsozView Answer on Stackoverflow
Solution 7 - Command LineShaik Mahaboob JohnyView Answer on Stackoverflow
Solution 8 - Command LineArindam NayakView Answer on Stackoverflow