Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leiningen with multiple main classes

I'd like to have two main classes (or more) with leiningen, and then be able to choose which one at the java command line. For example I have:

(ns abc (:gen-class))
(defn -main [] (println "abc"))

(ns def (:gen-class))
(defn -main [] (println "def"))

With a project.clj having:

(defproject my-jar "0.0.1"
 :description "test"
 :dependencies [
 ]
 :main abc)

Then I build with lein uberjar, and run:

java -cp my-jar-0.0.1-standalone.jar abc
java -cp my-jar-0.0.1-standalone.jar def

I get it that when I specified :main abc in the project.clj it was calling that out as the main-class in the manifest, but I couldn't get it to run without putting something. But either way when I try to run the 'def' main, I get a class not found:

Exception in thread "main" java.lang.NoClassDefFoundError: def
like image 715
Kevin Avatar asked Jun 13 '12 21:06

Kevin


1 Answers

This works at least with leiningen 2.0+

(defproject my-jar "0.0.1"
 :description "test"
 :dependencies [
 ]
 :profiles {:main-a {:main abc}
           {:main-b {:main def}}
 :aliases {"main-a" ["with-profile" "main-a" "run"]
           "main-b" ["with-profile" "main-b" "run"]})

Then you can run each main like so:

lein main-a
lein main-b

Which expands to this:

lein with-profile main-a run
lein with-profile main-b run

I'm using this in one of my projects and it works perfectly.

like image 167
Michael Avatar answered Nov 05 '22 17:11

Michael