Jar produced with lein uberjar fails on NoClassDefFoundError - clojure

I have a simple web app with this project.clj:
(defproject squirrel-money "1.0.0-SNAPSHOT"
:description "Squirrel Money"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[compojure "0.5.3"]
[ring/ring-jetty-adapter "0.3.5"]
[hiccup "0.3.1"]
[postgresql "8.4-701.jdbc4"]
[clj-time "0.2.0-SNAPSHOT"]]
:dev-dependencies [[lein-eclipse "1.0.0"]]
:main squirrel-money.main
:repl-init-script "src/squirrel_money/init_repl.clj")
My main looks like this:
(ns squirrel-money.main
(:gen-class)
(:use
[compojure.core]
[ring.adapter.jetty])
(:require
[compojure.route :as route]
[squirrel-money.savings :as savings]))
(defn launch [routedef]
(run-jetty routedef {:port 17080}))
(defroutes money-routes
(GET "/savings" [] (savings/render))
(route/not-found "Page not found"))
(defn -main [& args] (launch money-routes))
With REPL works just fine. However, when I generate a jar with lein uberjar and try to execute it as:
java -jar squirrel-money-1.0.0-SNAPSHOT-standalone.jar
It dies with this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: compojure/response/Renderable
at squirrel_money.main$fn__1067.invoke(main.clj:18)
at squirrel_money.main__init.load(Unknown Source)
at squirrel_money.main__init.<clinit>(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at clojure.lang.RT.loadClassForName(RT.java:1578)
at clojure.lang.RT.load(RT.java:399)
at clojure.lang.RT.load(RT.java:381)
at clojure.core$load$fn__4511.invoke(core.clj:4905)
at clojure.core$load.doInvoke(core.clj:4904)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.lang.Var.invoke(Var.java:365)
at squirrel_money.main.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: compojure.response.Renderable
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 13 more
What am I doing wrong? How to get it to work?
Not sure if that matters, but I noticed that inside the jar my files, clojure itself and Java libs are unpacked as .class files, while all clojure libs are present only as plain .clj files.

This seems to be a leinigen 1.4.0 bug. You might want to try creating an uberjar with leiningen 1.3.1.
Edit:
Leiningen 1.4.0 deletes non-project .class files to work around a Clojure bug (see CLJ-322). Apparently this behavior can sometimes cause problems.
You can keep leiningen 1.4.0 from deleting non-project .class files by setting :keep-non-project-classes to true in your project.clj.
See the related leinigen issue for more info.

Related

java.io.FileNotFoundException: Could not locate clojure/spec/alpha__init.class or clojure/spec/alpha.clj on classpath

I was trying to understand Clojure spec. While setting up a clojure project I am getting following error while requiring the clojure.spec.alpha:
Exception in thread "main" java.io.FileNotFoundException: Could not locate clojure/spec/alpha__init.class or clojure/spec/alpha.clj on classpath., compiling:
at clojure.lang.Compiler.load(Compiler.java:7391)
at clojure.lang.Compiler.loadFile(Compiler.java:7317)
at clojure.main$load_script.invokeStatic(main.clj:275)
at clojure.main$script_opt.invokeStatic(main.clj:335)
at clojure.main$script_opt.invoke(main.clj:330)
at clojure.main$main.invokeStatic(main.clj:421)
at clojure.main$main.doInvoke(main.clj:384)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:379)
at clojure.lang.AFn.applyToHelper(AFn.java:154)
at clojure.lang.Var.applyTo(Var.java:700)
at clojure.main.main(main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
My project.clj:
(defproject testing "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.9.0"]
[org.clojure/spec.alpha "0.1.143"]
[org.clojure/core.specs.alpha "0.1.10"]])
and core.clj:
(ns testing.core
(:require [clojure.spec.alpha :as s]))
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
I will be thankful, if someone can help me out.
I did a lein clean and it started working
For those who experience this problem on 1.9, see https://groups.google.com/forum/#!msg/clojure/-ovhQXtzhgw/XBm_rfcICgAJ
With 1.8, you can just run java -jar clojure-1.8.0.jar that you have in your .m2 and RELP will start instantaneously. With 1.9, it's no longer the case. You need additional libraries that you might not have in your .m2 by default.
You will have them though if you install you Clojure based on official guide.
I had the same error, I don't know clojure but had to fix some NoClassDefFoundError exceptions testing Kafka Storm using org.apache.storm.kafka.KafkaSpout.
First exception was: NoClassDefFoundError: clojure/lang/IFn
This I fixed by including clojure-1.9.0.jar in the classpath.
Next, this exception:
FileNotFoundException: Could not locate clojure/spec/alpha__init.class or clojure/spec/alpha.clj on classpath
later realized they have another jar spec.alpha-0.1.143.jar for this.
Hope this helps someone else!
What worked for me was adding test.check library as a dependency to my classpath

cannot run ring with lein

I am trying my first run with ring and lein, and I am facing problems in getting it to run. I have taken this example from the book "Web development with Clojure", chapter 1, and also from https://quickleft.com/blog/your-first-clojure-web-app/ . The code from both these sites give me the same error - Class Not Found.
I have the following project.clj
(defproject myfirstwebapp "0.1.1"
:description "A hello world for a Ring based web app"
:dependencies [[org.clojure/clojure "1.8.0"]
[ring "1.4.0"]]
:plugins [[lein-ring "0.9.7"]]
:dev-dependencies [[lein-ring "0.9.7"]]
:ring {:handler myfirstwebapp.core/app})
And the following core.clj
(ns myfirstwebapp.core)
(defn app [req]
{:status 200
:headers {"content-Type" "text/html"}
:body "Hello World!"})
And the commands I ran were these:
lein new myfirstwebapp
edit project.clj as above
cd myfirstwebapp
lein deps
edit src/myfirstwebapp/core.clj as above
lein ring server
And now I am getting errors like:
Exception in thread "main" java.lang.ClassNotFoundException: leiningen.core.project$reduce_repo_step, compiling:(C:\Users\ROG\form-init7789757414629005682.clj:1:17608)
Is there some mismatch between the versions of different components that I am using? Or something else?
It is a bug in lein 2.6.0. Fixed in 2.6.1

No main namepsace error while running a web app in Clojure using Noir

I'm using Noir.
This is my project.clj
(defproject noir "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]])
lein run gives me this error:
No :main namespace specified in project.clj.
Where am I going wrong?
Now, if I add :main my-website.server to project.clj, I get this error:
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: my-website.server
at clojure.lang.Util.runtimeException(Util.java:165)
at clojure.lang.RT.classForName(RT.java:2017)
at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:206)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:92)
at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:225)
at user$eval29.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:6465)
at clojure.lang.Compiler.eval(Compiler.java:6455)
at clojure.lang.Compiler.eval(Compiler.java:6431)
at clojure.core$eval.invoke(core.clj:2795)
at clojure.main$eval_opt.invoke(main.clj:296)
at clojure.main$initialize.invoke(main.clj:315)
at clojure.main$null_opt.invoke(main.clj:348)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Caused by: java.lang.ClassNotFoundException: my-website.server
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at clojure.lang.DynamicClassLoader.findClass(DynamicClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at clojure.lang.RT.classForName(RT.java:2013)
... 21 more
The problem is that lein don't know where to locate your -main function:
First, you shall create .clj file to run. You can specify it's namespace using ns macro. Then you shall define -main function in this namespace:
(ns my-website.server
(:require [noir.server :as server]
[noir.core :refer [defpage]]))
(defpage "/welcome" []
"Welcome to Noir!")
(defn -main
[& args]
(server/start 4000))
Then you shall configure your project.clj:
(defproject my-website "0.1.0-SNAPSHOT"
:description "..."
:dependencies [[org.clojure/clojure "1.4.0"]
[noir "1.2.2"]]
:main my-website.server)
[noir "1.2.2"] is the latest stable version of noir. It's best to use this one.
Do not forget to place this file into your source directory. Bu default its ./src dir in your project root. So, if your namespace is called my-website.server then lein will look for it in ./src/my-website/server.clj file (or in ./src/my_website/server.clj, I'm not sure).
Now lein run will cause lein to enter namespace my-website.server and then to run (-main) function.
See sample lein project for more info.
You can also generate project.clj for your noir project using lein noir template.

What Is The Reason For The Lein Cyclic Dependency Error When I build uberwar?

Building ring server-headless works -- lein ring server-headless -- but when I try to build the war or uberwar I get the following error, and cannot figure out why this is happening.
No namespaces to :aot compile listed in project.clj.
Exception in thread "main" java.lang.ExceptionInInitializerError, compiling:(ring/util/servlet.clj:62)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6416)
at clojure.lang.Compiler.analyze(Compiler.java:6216)
...
Caused by: java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at clojure.lang.RT.classForName(RT.java:2013)
at clojure.lang.Compiler$HostExpr.maybeClass(Compiler.java:938)
at clojure.lang.Compiler$HostExpr.access$400(Compiler.java:710)
at clojure.lang.Compiler.macroexpand1(Compiler.java:6342)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6395)
... 69 more
Caused by: java.lang.Exception: Cyclic load dependency: [ /servlet ]->/ring/util/servlet->[ /servlet ]
at clojure.core$check_cyclic_dependency.invoke(core.clj:5288)
at clojure.core$load.doInvoke(core.clj:5383)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:401)
at servlet.<clinit>(Unknown Source)
... 76 more
My project.clj file is:
(defproject myproj "0.1"
:description "the sample"
:dependencies [
[org.clojure/clojure "1.3.0"]
[compojure "1.0.4"]
[hiccup "1.0.0"]
[clj-json "0.5.0"]
[ring/ring "1.1.0"]
[clj-http "0.1.1"]
]
:plugins [
[lein-ring "0.7.0"]
]
:ring {:handler routes/start})
If I remove the :ring {:handler routes/start} then I get a NPE somewhere else.
I don't know if I'm missing something in my project.clj, or if the particular version of lein is broken for this use case. Can someone clarify this for me?
I had the same issue, in my case the solution was as simple as doing a
lein clean
before the
lein ring uberwar
I think actual issue is that at some point, my handler was pointing to an incorrect/inexistent handler and that left a servlet.clj in the target/classes directory. Doing a clean will remove this file.
I solved the issue, which was a bit of a mistake on my part. Posting the answer here in case anyone makes the same mistake.
I had something like the following in src/routes.clj:
(defroutes main-routes
(GET "/some/path" [& params] (some-code params))
(route/resources "/")
(route/not-found "not found"))
(def start (run-jetty (handler/site main-routes) {:port 8080}))
This is all standard code to setup routes and provide a hook to start the jetty webapp from the command line via lein ring server-headless. Except that I declared start as a global instead of a function. That means when I run lein ring server-headless things still work, but when I run lein ring uberwar I end up with a weird configuration--a full jetty server will try to start up with it's servlet, AND uberwar has generated a servlet and is trying to package it into a jar.
When I was comparing my code against the compojure examples I kept missing this difference I guess because def and defn optically look so similar. But anyway I just made this change to get it working:
(defn start [] (run-jetty (handler/site main-routes) {:port 8080}))
The error says it all, the :aot param is missing from the project config. Check out this link for using :aot.

command line arguments with leiningen

I have recently started out with clojure, and I am using leiningen to create a small project. I am having troubles in getting leiningen to behave with command line arguments. The following is my src/project/core.clj
(ns project.core
(:gen-class))
(defn -main [& args]
(println (apply str args)))
and my project.clj
(defproject project "1.0.0-SNAPSHOT"
:description "FIXME: write"
:main project.core
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]])
Now when I run lein run arg1 arg2, it gives me this error:
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: arg1 in this context (NO_SOURCE_FILE:1)
at clojure.lang.Compiler.analyze(Compiler.java:5205)
at clojure.lang.Compiler.analyze(Compiler.java:5151)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3057)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:5371)
at clojure.lang.Compiler.analyze(Compiler.java:5190)
at clojure.lang.Compiler.analyze(Compiler.java:5151)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:4670)
at clojure.lang.Compiler$FnMethod.parse(Compiler.java:4328)
at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3173)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:5367)
at clojure.lang.Compiler.analyze(Compiler.java:5190)
at clojure.lang.Compiler.eval(Compiler.java:5421)
at clojure.lang.Compiler.eval(Compiler.java:5415)
at clojure.lang.Compiler.eval(Compiler.java:5391)
at clojure.core$eval.invoke(core.clj:2382)
at clojure.main$eval_opt.invoke(main.clj:235)
at clojure.main$initialize.invoke(main.clj:254)
at clojure.main$null_opt.invoke(main.clj:279)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:422)
at clojure.lang.Var.invoke(Var.java:369)
at clojure.lang.AFn.applyToHelper(AFn.java:165)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
Caused by: java.lang.Exception: Unable to resolve symbol: arg1 in this context
at clojure.lang.Compiler.resolveIn(Compiler.java:5677)
at clojure.lang.Compiler.resolve(Compiler.java:5621)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:5584)
at clojure.lang.Compiler.analyze(Compiler.java:5172)
... 23 more
However, if I do a lein uberjar and then do java -jar project-1.0.0-SNAPSHOT-standalone.jar arg1 arg2, I get the correct output.
arg1arg2
It isn't very comfortable to have to create the uberjar to run it every time while development, is there a better way?
This looks like it's caused by a bug that's been fixed in git. The fix will be in 1.4.2, which should be out in a few days. In the mean time, you can use workarounds discussed here: http://groups.google.com/group/clojure/msg/a8160b23a5019a12
From lein-run: "Args will be passed on as *command-line-args*"
So you will have to use those. The example on the site shows how. If you now what arguments you are passing you an also use :run-aliases to specify those in your project.clj. Again, the mentioned site has all the information.
My sample project.clj
(defproject addressbook "1.0.0-SNAPSHOT"
:description "FIXME: write"
:main addressbook.core
:run-aliases {:addressbook [addressbook.core -main "arg1"]}
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]]
:dev-dependencies [[lein-run "1.0.0"]])
And the test code:
(ns addressbook.core
(:gen-class))
(defn -main [& [args]]
(if args (println args)))
Both "lein run addressbook foo" as "lein uberjar" work for me.