How can I run the .clj Clojure file that I created?

I installed Geany on my Linux Mint and wrote simple code. Just a little hello world to get started with language.

Now I would like to run it and see what it outputs.

How can I run this code? I do not use an IDE or anything that has been pre-packaged, just a simple Geany text editor.

Which command should I execute?

+51
command-line clojure
05 Oct. 2018-11-11T00:
source share
7 answers

You can run the script with the following command:

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

but itโ€™s better to use leiningen , especially when you start adding dependencies to your project. lein provides the number of commands to run your code (with all the necessary dependencies), the package code into the archive using lein jar or create complete independent archives with lein uberjar , which you can run with:

 java -jar your_app.jar 

Team

PS You can read how to use lein in the next article - it describes the basic tasks and configurations

+52
05 Oct '11 at 5:24 a.m.
source share

With clojure

I canโ€™t believe that no one suggested this. You should just do

 clojure path/to/script.clj 

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

With lein-exec

However, if the script you are working on has any dependencies, I would recommend the lein-exec plugin. This has the advantage of allowing Leiningen to be used to handle any dependencies that are enjoyable. Leiningen already has a lein run command, but it only works for basic functions in a complete Clojure / lein project. Thus, the lein-exec plugin is a really nice add-on for small scripts.

Dependency Information ...

Note. For thoroughness, if you use a lane, you can add any libraries that you would like to use in the ~/.lein/profiles.clj file, or in your project.clj file if you are working in a project. Then, when you run lein deps all the dependencies will be installed and available in your scripts / projects using lein exec / lein run . In addition, lein repl provides you with clojure repl with access to these dependencies. Again, definitely the way to go if you have any dependencies in the library.

In any case, consider using drip for faster startup.

Drip is a neat tool that provides you with a ready-to-boot, ready-to-run JVM image. This can be useful to reduce the startup time of JVM applications. clojure may take quite a while to configure itself, so a drip application is a good tool to speed up this process. This is especially true when you write small scripts that you usually expect to run quickly with. If you are using leiningen, check out the lein-drip plugin .

For ClojureScript

I would recommend http://planck-repl.org/ . It now supports (bootable) quick start of ClojureScript scripts without having to run JVM or Clojure. For most scripting tasks, fast execution is important, so when you donโ€™t need something specific for the JVM, this is my No. 1 recommendation.

+45
Aug 30 '13 at 4:41
source share

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

 lein exec hello.clj 

If you pass command line arguments like

 lein exec hello.clj arg1 arg2 arg3 

you can access them in the foo function in hello.clj, for example

 (foo *command-line-args*) 
+8
Feb 23 '15 at 14:13
source share

For one clj file you can add

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

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

 java -jar cool_app.jar 
+7
Oct 05 '11 at 4:14
source share

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

The cake was included in Leiningen and has since been replaced as the most stable implementation of Clojure Automation Drip - see this answer to a similar question here.

+1
Jan 13 '14 at 7:56
source share

I had a similar problem when running a specific clojure script file from a clojure project using lein . Then I found a shortcut, thought about sharing the same.

In the project.clj file, you can switch the name of the clojure script file (which has the main method, of course). Below is the instruction for this.

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

Note: I'm new to clojure, and I'm not sure why "^: skip-aot` is used, I have not tested this.

Note. . This methodology does not require the installation of any plugin, and it is not necessary to create any jar file. It only requires a name change in the project file.

Assumption: There are several clojure files in the clojure project, each of which has a main method. I assume this is for test purposes only. My solution will work for this particular scenario.

0
Aug 02 '16 at 10:09 on
source share

if you just want to execute a single file, how about linking it to a replica, for example:

cat $PATH_TO_FILE | lein repl

0
Sep 18 '16 at 13:12
source share



All Articles