LinuxCommandLibrary

clj

Run Clojure programs or start a REPL

TLDR

Start a REPL (interactive shell)

$ clj
copy

Execute a function
$ clj -X [namespace/function_name]
copy

Run the main function of a specified namespace
$ clj -M [[-m|--main]] [namespace] [args]
copy

Prepare a project by resolving dependencies, downloading libraries, and making/caching classpaths
$ clj -P
copy

Start an nREPL server with the CIDER middleware
$ clj -Sdeps '{:deps {nrepl {:mvn/version "0.7.0"} cider/cider-nrepl {:mvn/version "0.25.2"]}' [[-m|--main]] nrepl.cmdline --middleware '["cider.nrepl/cider-middleware"]' --interactive
copy

Start a REPL for ClojureScript and open a web browser
$ clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "1.10.758"]}' [[-m|--main]] cljs.main [[-r|--repl]]
copy

SYNOPSIS

clj [options] [arguments]

PARAMETERS

-Sdeps edn-map
    Use specified dependency map.
Overrides dependencies listed in the deps.edn file.

-Srepro
    Enter a REPL without reading a program from STDIN or file.

-Scopy-deps
    Prints the class path dependencies to standard output. Useful for integration with other tools.

-Spath
    Prints class path to standard output, and exits.

-Sforce
    Force dependency refresh.

-Aalias-map
    Alias expansion map.

-Ttool-alias
    Invoke specified tool alias.

-Xalias-map
    Run specified tool alias.

-Jjvm-option
    Pass jvm options to clojure invocation.

file
    Execute Clojure code from a file.

-
    Execute Clojure code from standard input (STDIN).

'clojure-code'
    Execute the given clojure-code directly.

DESCRIPTION

The `clj` command is a versatile tool designed to execute Clojure code directly from your terminal. It leverages the Clojure CLI tools to compile, load, and run Clojure scripts or programs specified as arguments. This allows for rapid prototyping, scripting, and the execution of Clojure-based utilities without the need for a full-fledged project setup.
`clj` effectively bootstraps a Clojure environment, handling classpath management and dependencies behind the scenes, simplifying the process of working with Clojure code. It supports various input methods, including direct code execution, file loading, and REPL interaction. Users can leverage it for tasks ranging from simple data transformations to more complex application logic.
The command implicitly uses `deps.edn` files (if present) for dependency management, enabling easy integration with existing Clojure projects. This makes it an invaluable tool for Clojure developers, scripting enthusiasts, and anyone seeking to integrate Clojure code into their command-line workflow. Its ease of use and powerful features make it a valuable asset for both beginners and experienced Clojure developers.

DEPENDENCIES

The `clj` command utilizes `deps.edn` files to manage dependencies. If a `deps.edn` file is present in the current directory or parent directories, `clj` will automatically resolve and include the specified dependencies in the classpath.

REPL

With the `-Srepro` command option a REPL (read-eval-print loop) can be started. This allows for interactive evaluation and testing of Clojure code.
The REPL is a vital part of Clojure development, enabling immediate feedback and experimentation.

HISTORY

The `clj` command is part of the Clojure CLI tools, a relatively recent development aimed at simplifying Clojure development workflows. It provides a streamlined way to execute Clojure code without needing a full-fledged project setup with build tools like Leiningen or Boot. Its development stemmed from the need for a more lightweight and flexible approach to running Clojure code, especially for scripting and experimentation. The `clj` command has become increasingly popular due to its ease of use and integration with the Clojure ecosystem, enabling faster development cycles and easier adoption of Clojure.

SEE ALSO

clojure(1), java(1)

Copied to clipboard