5

Is it possible to compile clojure source without going into REPL? If a big project is there then it is not possible to compile each program manually & then make jar file of it, like if i wish to compile and get class files by some set of instructions in make software?

5
  • 1
    Why do you need to use make and cannot, for instance, use leiningen or maven?
    – skuro
    Commented Oct 10, 2011 at 11:28
  • @skuro [stackoverflow.com/questions/7686452/… this is the of configuring a lein on windows, following same set of instructions i am not able to configure it properly.
    – vikbehal
    Commented Oct 10, 2011 at 11:31
  • Even in make you will have to follow the same steps, like providing a -main function and the like. Only, there's hardly any other clojure developer using such a toolset, while there are plenty of happy leiningen users. I'd suggest you to post your leiningen configuration and ask for help on that.
    – skuro
    Commented Oct 10, 2011 at 12:01
  • I know Make, but not clojure. It sounds as if Make isn't the best tool for the job; if you still want to use it, tell us about the commands you would use to do it manually, and we can tell you how to delegate them to Make.
    – Beta
    Commented Oct 10, 2011 at 12:25
  • possible duplicate of How do I create an executable file in clojure? Commented Oct 10, 2011 at 12:32

3 Answers 3

8

For the sake of understanding how many of these systems work underneath, here is some bare minimum code to compile code without a repl.

Say you have some class generating code:

hello.clj:

 (ns hello
   (:gen-class
    :methods [[sayHi [] String]]))

 (defn -sayHi [this]
   (println "hello world"))

You can build your "makefile" out of clojure code

compile.clj:

 (set! *compile-path* "./")
 (compile 'hello)

Then you just call your code as a script.

 $ java -cp ~/dj/lib/clojure.jar:./ clojure.main compile.clj 
 $ ls
 compile.clj  hello.clj          hello$loading__4505__auto__.class
 hello.class  hello__init.class  hello$_sayHi.class

Now your code is compiled and you can access it like any other class file:

 $ java -cp ~/dj/lib/clojure.jar:./ clojure.main
 Clojure 1.3.0
 user=> (import 'hello)
 hello
 user=> (.sayHi (hello.))
 "hello world"
 user=> 
5
  • 1
    is it possible to call hello.clj without going into REPL? like __java -cp clojure.jar clojure.main -i "hello.clj" -e '(hello)' __
    – vikbehal
    Commented Oct 12, 2011 at 5:01
  • 1
    Yes, just use the same technique I used with writing the compile script. Create a clojure file with the code you want to run, then run $ Java -cp clojure.jar clojure.main runhello.clj
    – bmillare
    Commented Oct 12, 2011 at 5:25
  • writing (import 'hello) (.sayHi (hello.)) in a file and running like you said isn't helping, getting no output, no error
    – vikbehal
    Commented Oct 12, 2011 at 5:43
  • My class outputs a string to repl. To print you need to actually call print. You need to rewrite hello.clj
    – bmillare
    Commented Oct 12, 2011 at 13:03
  • 1
    I've made the changes to so that it will print when run from command line.
    – bmillare
    Commented Oct 12, 2011 at 15:20
4

Yes - you can certainly compile and run Clojure code without using a REPL.

Some options to do this:

  • Load the Clojure code at runtime using the load function. This will read, compile and evaluate the Clojure code specified without any REPL involved.
  • Package everything as a .jar using Leiningen or Maven or any other suitable build tool - then you can run your code as a regular Java application, the Clojure code will be compiled and run when the .jar is executed
  • Launch the code from Java - write a Java application that launches the Clojure compiler directly to compile and then execute the Clojure code. This might be a sensible approach if you are using Clojure as a component of a larger Java application.
2
  • How can we load a file at run time without any REPL? Don't we need to do this? user=> (load-file "filename.clj")?
    – vikbehal
    Commented Oct 11, 2011 at 9:02
  • 1
    You can execute this code from other code, perhaps based on some other form of user input (e.g. selecting the Clojure file to run in a GUI).
    – mikera
    Commented Oct 11, 2011 at 10:05
3

I have a large project using many languages and use make as the glue to start all the other components and this works quite well. You may do well to have make call maven or leiningen instead of using make to reinvent them.

If you are looking for an alternative to leiningen because it is not working for you or you simply wish to use something else, then perhaps you will like the Clojure Maven plugin

Personally I can't speak too highly of Leiningen and it is my personal choice, though there are many people in the internet and it's worth exploring the other options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.