cannot run ring with lein - clojure

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

Related

Leiningen Bug In Producing Uberjars Or Misunderstanding?

What can cause lein uberjar to fail with "Caused by: java.lang.ClassNotFoundException" errors when lein run runs my app successfully? I'm using leiningen 2.5.1.
It looks like leiningen provides profiles as a way to make custom environments for certain tasks. However, I don't believe a profile difference between what is happening with lein run and lein uberjar is responsible because ...
1) I tried producing the uberjar with every possible profile, via lein with-profile <profile> for every <profile> named in the output of lein show-profiles.
2) I ran lein with-profile uberjar run, and no errors arose.
In case the answer to this problem and similar problems might be specific to the classes and dependencies involved, it is classes related to Overtone that produce the ClassNoteFoundException errors.
My project.clj file looks like:
(defproject cochlea "0.1.0-SNAPSHOT"
:description "An interactive ear training GUI application"
:url "http://github.com/seewalker/cochlea"
:license {:name "GPL v3"
:url "http://www.gnu.org/copyleft/gpl.html"}
:repositories {"conjars" "http://conjars.org/repo"}
:dependencies [[org.clojure/clojure "1.5.1"]
[overtone "0.9.1"]
[org.clojure/tools.trace "0.7.5"]
[org.clojure/java.jdbc "0.3.7"]
[postgresql "9.1-901.jdbc4"]
[environ "0.5.0"]
[incanter "1.9.0"]
[clj-time "0.9.0"]
[me.raynes/conch "0.8.0"]
[seesaw "1.4.2" :exclusions [org.clojure/clojure]]]
:profiles { :uberjar {:aot :all} }
:repl-options { :timeout 120000}
:main cochlea.core
:target-path "target/%s")

LightTable not connecting/evalling a Clojure project

Just starting a new Clojure project and running into some issues getting LightTable to play nicely. The app/REPL spin up fine with lein ring server / lein repl etc, but trying to connect to any file in LightTable just keeps the blue spinner…spinning…even after it says it's connected.
These are my deps (with LightTable 1.5.4 - just updated from 1.4.something which was doing the same thing)
(defproject clollo "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.5.1"]
[compojure "1.1.5"]
[trello "0.1.1-SNAPSHOT"]]
:plugins [[lein-ring "0.8.5"]]
:ring {:handler clollo.handler/app}
:profiles
{:dev {:dependencies [[ring-mock "0.1.5"]]}})
Files in question are
(ns clollo.handler
(:use compojure.core)
(:require [compojure.handler :as handler]
[compojure.route :as route]
[trello.core]))
(defroutes app-routes
(GET "/" [] "Hello Wo1rld")
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
(+ 1 1) ;; test LightTable eval!
I then tried getting rid of all the requires etc for debugging, so I have a new file
(ns clollo.ihopethisworks)
(+ 1 1) ;; this doesn't work either
Any ideas? As I say, LightTable's InstaRepl works fine on its own, and LightTable is connecting to this project's project.clj in the connect window. Just spinning when it tries to eval anything.
Thanks!
From testing your sample code, it looks like the [trello.core] require item, in the handler.clj file, is causing the issue. The library seems to have an issue with initializing clj-http in Light Table.
Also, there is an issue, with LT v0.5.x, where the working indicator, in some cases, keeps spinning after the operation is complete. You can use the Statusbar: Reset working indicator command to stop it.

How to Run Jetty Example with Ring in Clojure

I am following along with this example on creating a simple web service in Clojure using ring and jetty.
I have this in my project.clj:
(defproject ws-example "0.0.1"
:description "REST datastore interface."
:dependencies
[[org.clojure/clojure "1.5.1"]
[ring/ring-jetty-adapter "0.2.5"]
[ring-json-params "0.1.0"]
[compojure "0.4.0"]
[clj-json "0.5.3"]]
:dev-dependencies
[[lein-run "1.0.0-SNAPSHOT"]])
This in script/run.clj
(use 'ring.adapter.jetty)
(require '[ws-example.web :as web])
(run-jetty #'web/app {:port 8080})
And this in src/ws_example/web.clj
(ns ws-example.web
(:use compojure.core)
(:use ring.middleware.json-params)
(:require [clj-json.core :as json]))
(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})
(defroutes handler
(GET "/" []
(json-response {"hello" "world"}))
(PUT "/" [name]
(json-response {"hello" name})))
(def app
(-> handler
wrap-json-params))
However, when I execute:
lein run script/run.clj
I get this error:
No :main namespace specified in project.clj.
Why am I getting this and how do I fix it?
You're getting this error because the purpose of lein run (according to lein help run) is to "Run the project's -main function." You don't have a -main function in your ws-example.web namespace, nor do you have a :main specified in your project.clj file, which is what lein run is complaining about.
To fix this, you have a few options. You could move the run-jetty code to a new -main function of the ws-example.web function and then say lein run -m ws-example.web. Or you could do that and also add a line :main ws-example.web to project.clj and then just say lein run. Or you could try using the lein exec plugin to execute a file, rather than a namespace.
For more info, check out the Leiningen Tutorial.
You have to put that (run-jetty) stuff into a -main somewhere and then add it to the project.clj like
:main ws-example.core)
From lein help run:
USAGE: lein run -m NAMESPACE[/MAIN_FUNCTION] [ARGS...]
Calls the main function in the specified namespace.
So, you would need to put your script.clj somewhere on the project source path and then call it as:
lein run -m script

How to define project.clj for both lein run and lein repl to work?

I'm new to Clojure, and I don't quite understand how to write my project.clj so it works for both lein repl and lein run. Here it is (whole path: ~/my-project/project.clj):
(defproject my-project "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]]
:main my-project.core/hello
)
Then I have my ~/my-project/src/my_project/core.clj file
(ns my-project.core)
(defn hello []
(println "Hello world!")
)
lein run works just fine but I get a FileNotFoundException when running lein repl:
~/my-project$ lein run
Hello world!
~/my-project$ lein repl
REPL started; server listening on localhost port 42144
FileNotFoundException Could not locate hello__init.class or hello.clj on classpath: clojure.lang.RT.load (RT.java:430)
clojure.core=>
How should I edit the project.clj to solve this? Or do I have to call lein repl in a different way?
Thanks in advance.
EDIT: tried with lein deps and lein compile, but still the same error
~/my-project$ lein version
Leiningen 1.7.1 on Java 1.6.0_27 OpenJDK Client VM
~/my-project$ lein deps
Copying 1 file to /home/yasin/Programming/Clojure/my-project/lib
~/my-project$ lein compile
No namespaces to :aot compile listed in project.clj.
~/my-project$ lein repl
REPL started; server listening on localhost port 41945
FileNotFoundException Could not locate hello__init.class or hello.clj on classpath: clojure.lang.RT.load (RT.java:430)
One thing you could do to get it to work would be to change core.clj to:
(ns my-project.core
(:gen-class))
(defn hello []
(println "Hello world!"))
(defn -main []
(hello))
And edit the project.clj to:
(defproject my-project "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]]
:main my-project.core)
The (:gen-class) will tell the compiler to generate a Java class for the namespace, and the :main directive in project.clj will tell lein run to run the main method on the class, which is given by -main. Why lein repl was failing to find my-project.core/hello is unclear to me, but I don't know much about leiningen internals.

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.