简体   繁体   中英

Clojure: How to run my main from jar?

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

如果您使用leiningens uberjar任务打包您的应用程序,那么您需要做的就是运行,

java -jar name-of-your-app.jar

This is possible now [1] if you can generate a correct classpath (eg 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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM