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

1

11

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
Trona answered 25/12, 2012 at 4:49 Comment(2)
It looks like you don't have a -main function in my-website.server. How did you create the project? If you do lein noir new my-website it sets up project.clj and creates a default -main function in server.cljSinter
That's what I did. lein noir new appnameTrona
V
9

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.

Volding answered 25/12, 2012 at 5:32 Comment(7)
This .clj file should be called what? (Sorry, just starting up and there is a bit of a caveat of documentation to smoothen this out.)Trona
Did that. Current error: Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: defpartial in this context, compiling:(noir/content/defaults.clj:8)Trona
It's some error in server.clj. lein found it, but failed to compile.Volding
I fixed my example a little bit, try it now. Unfortunately, I cant test it myself right now.Volding
If it won't help, try to change noir version to [noir "1.2.2"]. 1.2.2 is stable.Volding
I think you should put it in a separate answer and I will accept it as an answer to help others.Trona
I've got to go. I'll put it in a separate answer later if you insist, but for me it looks better to gather all useful information in one place.Volding

© 2022 - 2024 — McMap. All rights reserved.