I have my Clojure app packed in a jar. I am launching it as:
java -cp lib/clojure-1.2.0.jar:my-app.jar clojure.main -e "(use 'foo.main)(-main)"
Is this the only way to launch my application from this jar? Specifically, I would love to see something as simple as:
java -cp lib/clojure-1.2.0.jar:my-app.jar clojure.main foo.main
Use
(ns foo.main
(:gen-class)
in your main source to generate the namespace package qualified class file. Add
:main foo.main
:manifest {"Class-Path" "lib/clojure-1.2.0.jar"}
to your Leiningen project file. After "lein jar" you can startup the application using the metadata from the JAR-embedded Manifest file:
java -jar foo-YOURVERSION.jar
If you pack your application with leiningens uberjar task then all you need to do is run,
java -jar name-of-your-app.jar
This is possible now [1] if you can generate a correct classpath (e.g. from Leiningen):
java -cp $(lein classpath) clojure.main -e "(do '(require '[clojure.string]) (println (clojure.string/join \" \" [1 2 3])))"
Or from an uberjar:
java -cp my_uberjar.jar clojure.main -e "(do '(require '[clojure.string]) (println (clojure.string/join \" \" [1 2 3])))"
You can also do something similar from another Clojure process itself (e.g. the repl) using this library https://github.com/clojure/java.classpath
(clojure.java.shell/sh "java" "-cp" (clojure.string/join ":" (map #(.toString %) (clojure.java.classpath/classpath) "clojure.main" :in (pr-str '(do (require '[my.namespace]) ([my.namespace/my-main)))))
[1] Not sure when this was introduced
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