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

前端 未结 8 1768
夕颜
夕颜 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 08:38

    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")
    
    0 讨论(0)
  • 2021-01-30 08:41

    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.

    0 讨论(0)
  • 2021-01-30 08:43

    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*) 
    
    0 讨论(0)
  • 2021-01-30 08:47

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

    cat $PATH_TO_FILE | lein repl

    0 讨论(0)
  • 2021-01-30 08:47

    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.

    0 讨论(0)
  • 2021-01-30 08:48

    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

    0 讨论(0)
提交回复
热议问题