views:

184

answers:

1

It seems that to install Clojure in every new IDE, I have to completely re-install it and create a copy of it. And running the REPL seems like it's running a Java program.

I'm coming from a Ruby background, where Ruby programs are run by ruby program.rb, and the ruby being a program executed from one place, onto the file (I am aware that this is similar to how it works for java, python, etc. as well).

Is the clojure "interpreter" merely a compiled Java file which takes the .clj file as an argument, and does stuff to it?

+15  A: 

Firstly, Clojure has no interpreter. All Clojure code is compiled into JVM bytecode when it is being loaded. I'm stressing this point, because this is were Clojure's excellent performance story begins.

Secondly, you don't really "install" Clojure in the sense that you do Ruby. Clojure comes as a jar file, which is just a bunch of Java classes; if you put the jar file on your classpath, you can run methods of those classes. Of those classes, clojure.main provides a main method for running REPLs and "scripts". So, running the REPL is indeed running a Java (that is, JVM) programme; and running a clj file amounts to asking clojure.main to load and run it (the actual work is handed off to other classes in Clojure's implementation, but clojure.main is the entry point). BTW, this is exactly the same as with JRuby.

Every JVM programme is ultimately "merely a compiled Java file", or perhaps a bunch of such files. To run it, you need to have a JVM instance load it and run the appropriate main method. Note that C programmes (such as ruby-the-command) are only different in that the operating system knows how to find their main functions for you (well, the equivalent of Java's classpath works pretty differently too, but the main concepts are the same). With JVM programmes, you need to use an OS-friendly executable (java / java.exe) to kick things off.

Michał Marczyk
When you load a .clj file into `clojure.main`, what happens to the actual file? You mention it being "compiled" into JVM bytecode...is this to a point where that JVM code is outputted as an extra file, and that file run by the JVM itself?
Justin L.
Normally no actual `.class` files are produced, though you can ask for them if you want (see `(doc compile)` and `(doc *compile-files*)`). There's no reason to worry about this most of the time. Note that this mode of operation is not particular to Clojure at all; Python does the same thing, compiling `.py` files to Python bytecode and then running it. One difference is that Python outputs the compiled modules as `.pyc` files by default, but this isn't even always possible (e.g. due to permission issues) and has no bearing on the way in which the code is executed.
Michał Marczyk
If I create a `.class` file, and handed it to Bob, would he be able to run it without installing clojure?
Justin L.
As mentioned in the answer, you don't "install" Clojure, you simply put it on the classpath. Having said that, there are fairly straightforward methods of packaging the Clojure runtime together with your code in a single `jar`, so that Bob never needs to know it's been written in Clojure. You can also package your code in its own `jar` and send Bob a collection of `jar`s including `clojure.jar` (put it all in a single archive together with a launcher script and, again, Bob never needs to care what language it's been written in). See Leiningen: http://github.com/technomancy/leiningen
Michał Marczyk
Clojure deployment is basically JVM deployment and there's loads and loads of tools and conventions to do with that. For organisations using Java, getting a bunch of `jar`s (or `war`s, as the case may be) is fantastic, because they know exactly what to do with them (and never need to care there's something called "Clojure" doing work inside them). For people coming from Ruby, this represents an area where new things have to be learned, but judging by the success of many prominent members of the Clojure community who used Ruby previously (or indeed still use it now), it isn't too big a problem.
Michał Marczyk
To answer your .class file question specifically Bob would need the clojure jars to run it since the class file generated has a dependency on them. But that's no different than if I were to create a .class file in java that had a dependency on some other java library.
Jeremy Wall
Thanks; these have been really helpful :) I like the dependency relationship; it really clears things up.
Justin L.