I can’t for the life of me seem to figure out what to do with a clojure program after I have written it. How do I take a .clj file and turn it into a .class?
I don’t want to set up a leingen project or whatever, I don’t want to bundle it as a jar, all I want to do is write a .clj file in a text editor and turn it into a .class so I can run it like java ClassName
Author of the article here. Clojure is more commonly loaded and compiled on the fly, so the easiest way to run a single .clj file is to just do clojure your_file.clj. This uses the official Clojure CLI. Compiling to class files (AOT compilation) is usually reserved for production deployments, where you build an (uber)jar.
That said if you want to just compile a single namespace, you do that with Clojure’s compile function. It’ll look for the namespace on the classpath (defaults to src), and write the resulting classes to classes.
Why do you want it to run like java ClassName in the first place? To me that seems a bit arbitrary, and really doesn’t work unless you add the Clojure runtime on the classpath.
Though I assume you just want to run a .clj file as a command on the command line, and there Babaskha is the de-facto standard tool to that these days.
Another aspect that helps here is that Clojure generally avoids polymorphism.
Maybe the author meant that it avoids inheritance?
Clojure’s polymorphism story is IMO one of the nicest features of the language. Protocols offer true composition over inheritance, whereas in most languages you still have to know which concrete class to instantiate ahead of time to take advantage of your own extension. But with protocols you can just say “abstraction X behaves this way when .someMethod gets called on it,” regardless of whether X was defined in your code of some third-party lib. I miss this in every other language.
I can’t for the life of me seem to figure out what to do with a clojure program after I have written it. How do I take a
.cljfile and turn it into a.class?I don’t want to set up a leingen project or whatever, I don’t want to bundle it as a jar, all I want to do is write a .clj file in a text editor and turn it into a .class so I can run it like
java ClassNameAuthor of the article here. Clojure is more commonly loaded and compiled on the fly, so the easiest way to run a single
.cljfile is to just doclojure your_file.clj. This uses the official Clojure CLI. Compiling to class files (AOT compilation) is usually reserved for production deployments, where you build an (uber)jar.That said if you want to just compile a single namespace, you do that with Clojure’s
compilefunction. It’ll look for the namespace on the classpath (defaults tosrc), and write the resulting classes toclasses.Then from the shell:
You’ll need the Clojure and Spec jars to be able to run this:
Why a
.class? I use this trick to make my.cljfile executable:https://gist.github.com/yogsototh/dc16e3213115c8305720f8c74d4a8297
If you’re using
deps.ednto build your clojure project you probably wanttools.buildto produce an uberjar. https://clojure.org/guides/tools_build#_compiled_uberjar_application_buildWhy do you want it to run like
java ClassNamein the first place? To me that seems a bit arbitrary, and really doesn’t work unless you add the Clojure runtime on the classpath.Though I assume you just want to run a .clj file as a command on the command line, and there Babaskha is the de-facto standard tool to that these days.
Here’s a raw way with cli-tools:
Obviously, if you want an easier way of setting up the classpath, then the tooling is there for you. But if you don’t want to use it…
Maybe the author meant that it avoids inheritance?
Clojure’s polymorphism story is IMO one of the nicest features of the language. Protocols offer true composition over inheritance, whereas in most languages you still have to know which concrete class to instantiate ahead of time to take advantage of your own extension. But with protocols you can just say “abstraction X behaves this way when .someMethod gets called on it,” regardless of whether X was defined in your code of some third-party lib. I miss this in every other language.
And multimethods are just chef’s kiss