lein-ring icon indicating copy to clipboard operation
lein-ring copied to clipboard

'lein uberjar' works; 'lein ring uberjar' fails with Main-Class missing error

Open csdrane opened this issue 10 years ago • 9 comments

Using the following project.clj, I receive the following error when trying to compile with lein ring uberjar.

(defproject temp "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [org.clojure/java.jdbc "0.3.5"]
                 [compojure "1.1.9"]
                 [crypto-password "0.1.3"]
                 [hiccup "1.0.5"]
                 [korma "0.4.0"]
                 [mysql/mysql-connector-java "5.1.32"]
                 [ring "1.3.1"]
                 [ring/ring-defaults "0.1.1"]]
  :plugins [[lein-ring "0.8.11"]]
  :ring {:handler temp.core/app}
  :uberjar "amzn.jar"
  :main ^:skip-aot temp.core
  :aot [temp.core]
  :target-path "target/%s"
  :resource-paths ["resources"]
  :profiles {:uberjar {:aot :all}
             :dev {:plugins [[cider/cider-nrepl "0.7.0"]]}})
Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method.

I receive no such error and am able to launch a ring server successfully using just lein uberjar.

csdrane avatar Oct 08 '14 17:10 csdrane

This should be fixed in 0.8.12 (see this PR). Try updating your dependencies to the latest version.

weavejester avatar Oct 08 '14 17:10 weavejester

Unfortunately, no, this did not fix the problem.

On Wed, Oct 8, 2014 at 1:08 PM, James Reeves [email protected] wrote:

This should be fixed in 0.8.12 (see this PR https://github.com/weavejester/lein-ring/pull/127. Try updating your dependencies to the latest version.

— Reply to this email directly or view it on GitHub https://github.com/weavejester/lein-ring/issues/129#issuecomment-58391699 .

csdrane avatar Oct 08 '14 18:10 csdrane

I was able to diagnose the issue. The problem is resolved when I add :main project.core to the :ring map in project.clj.

My guess is that this part of the code (uberjar.clj) is failing for some reason:

(defn default-main-namespace [project]
  (let [handler-sym (get-in project [:ring :handler])]
    (str (namespace handler-sym) ".main")))

csdrane avatar Oct 08 '14 18:10 csdrane

I'm running into this issue, and @csdrane's fix doesn't appear to fix it. Here's my minimal repro:

project.cljs:

(defproject app "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [ring "1.3.2"]]

  :main ^:skip-aot app.core

  :target-path "target/%s"

  :plugins [[lein-ring "0.9.6"]]

  :ring {:handler app.core/handler}

  :profiles {:uberjar {:aot :all}})

src/app/core.clj:

(ns app.core)

(defn handler [request] {:status 200
   :headers {"Content-Type" "text/html"}
   :body "Hello World"})

per the above comment, i also tried with this in project.clj:

  :ring {:handler app.core/handler
         :main app.core}

output:

$ lein clean; lein ring uberjar
Compiling app.core
Compiling app.core
Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method.
$ lein -v
Leiningen 2.5.1 on Java 1.8.0_25 Java HotSpot(TM) 64-Bit Server VM

sodiumjoe avatar Jun 26 '15 04:06 sodiumjoe

I tried downgrading leiningen, with different but similar results:

$ lein -v
Leiningen 2.4.1 on Java 1.8.0_25 Java HotSpot(TM) 64-Bit Server VM
$ lein clean; lein deps; lein ring uberjar
Compiling app.core
Compiling app.core
Created /Users/jmoon/play/tmp/target/uberjar+uberjar/app-0.1.0-SNAPSHOT.jar
Created /Users/jmoon/play/tmp/target/uberjar+uberjar/app-0.1.0-SNAPSHOT-standalone.jar
$ java -jar target/uberjar+uberjar/app-0.1.0-SNAPSHOT.jar
Error: Could not find or load main class app.core.main
$ java -jar target/uberjar+uberjar/app-0.1.0-SNAPSHOT-standalone.jar
Error: Could not find or load main class app.core.main

sodiumjoe avatar Jun 27 '15 06:06 sodiumjoe

my workaround is to just use lein uberjar with a core.clj file like this:

(ns app.core
  (:gen-class)
  (:require [ring.adapter.jetty :as jetty]))

(defn handler [request]
  {:status 200
   :headers {"Content-Type" "text/html"}
   :body "Hello World"})

(defn -main []
  (jetty/run-jetty handler {:port 3000}))

sodiumjoe avatar Jun 27 '15 06:06 sodiumjoe

Interesting. It looks like the problem is the :target-path directive -- if I remove that, everything works perfectly. :target-path the way you're using it seems to be legitimate though (it's in the lein sample.project.clj) so I think we're doing something wrong with the target path (the target/uberjar+uberjar+uberjar+.... suggests this too)

MichaelBlume avatar Jun 27 '15 20:06 MichaelBlume

yeah, i thought that was a little weird, too, but I'm a clojure and jvm n00b

sodiumjoe avatar Jun 27 '15 20:06 sodiumjoe

OK, so part of the problem is that we're merging in the uberjar profile and then passing the project to the uberjar command, but when I fix that, I still see ubjerjar+uberjar going on, and honestly at that point I would love some help from someone who knews leiningen internals better than I do.

MichaelBlume avatar Jun 27 '15 21:06 MichaelBlume