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

前端 未结 8 1769
夕颜
夕颜 2021-01-30 08:19

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 w

相关标签:
8条回答
  • 2021-01-30 09:00

    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.

    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. For most scripting tasks, quick execution is important, so when you don't need anything specific to the JVM, this is my #1 recommendation.

    For bigger projects, not just repl/cli style usage, 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.

    drip for faster start time

    NOTE: I haven't tried to use this thing in years, so no idea if it still works, or how well maintained it is.

    Drip was(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.

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题