I've been learning clojure on the REPL environment with Clojure Box.
How can I make an executable file (.jar)?
I'm wondering if something like this is possible:
write clojure code on notepad and name it project.clj
compile project.clj
get executable file
Step #2 doesn't have to be done in command line. IDE is fine.
As Erhan Bagdemir answered, you should use Leiningen. Here's how I do it on Windows:
You're going to download the Leiningen script and put it somewhere on your PATH
. If you have a folder already on your path that you use for executable scripts, if not, do the following:
/Users/vikbehal
)PATH
. To do this, open the Control Panel, and go to the System area where you can edit your Environment Variables. Edit either the system or your user's PATH
value by adding /Users/vikbehal/lein
(or whatever the folder is called) to your PATH
, making sure to keep that entry separated from others by a semicolon (;
).Ok
to save your changes.You can verify that you've successfully installed Leiningen by opening up a command prompt and running lein help
. You should see Leiningen installing some initial files. If you get an error, make sure you've properly added the folder where Leiningen lives to your PATH
.
Leiningen gives you a good default directory structure, handles dependency management, and ensures that Java's classpath is set properly when running a REPL or, in this case, jarring your application.
Here's a run-down of Leiningen standard commands:
# Start a blank project
lein new my-project
# Pull down dependencies defined in your project.clj file
lein deps
# Create a "library" jar of your project
lein jar
# Create an executable jar of your project
lein uberjar
It looks like that last command is what you want, right? First, you need to configure a few things, so that Leiningen can build your executable jar properly.
You need to do three things to make lein uberjar
work properly:
-main
function that will act as the "entry point" to the execution of your jar file. (Note the -
, it denotes that this function is more like a "Java method" than a regular Clojure function.)(:gen-class :main true)
declaration in the ns
namespace declaration at the top of the file that contains your -main
function.:main my.main.namespace
entry in your project.clj
Now run lein uberjar
. While developing, you can alternatively use lein run
if you want to test how your application would behave as an executable jar, but don't want to explicitly re-jar every time you make a change.
Use Leiningen to create an uberjar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With