15

I'm still fairly new to Clojure so I apologize if this is a completely newbie question but I wasn't able to find a sufficient answer online.

Basically, my problem is that any time I try to run my project, I get an error like:

Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: Could not locate greeter__init.class or greeter.clj on classpath: 

In this case, greeter.clj is in the project in the same directory as the file containing my main function.

For illustration purposes, I've created a project that has a directory tree like this:

enter image description here

My code for core.clj is as follows:

(ns omg.core
(require [greeter]))

(defn -main[] (greet))

My code for greeter.clj is:

(ns greeter)

(defn greet [] println("Hello world"))

Whenever I type lein run -m omg.core I get the exception mentioned above. What am I doing wrong?

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81

1 Answers1

18

the greeter namespace it at the wrong level

(ns omg.greeter)

The names in namespace correspond to the folders in the path so to use the file in /src/omg/greeter.clj that file should contain the omg.greeter namespace. if you want to have it just called greeter then move it down one folder

When using require you need to spell out the namespace of the function you are calling, in this case that would be (omg.greeter/greet). since this is a pain, the use function requires a namespace and adds all it's functions to the current namespace. Another option that is more selective is to use require with a :refer option in the namespace definition

(ns omg.core
    (require [omg.greeter :refer :all]))

or

(ns omg.core
    (require [omg.greeter :refer [greet]]))

Most people put the namespace requirements into the ns call at the top of the file.

a quick read of http://clojure.org/namespaces will hopefully help

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • 1
    Arthur, thanks so much for your answer and your explanation. I really appreciate it. I got it working by stripping out the require and adding (use omg.greeter) in its place. – Carl Veazey May 02 '12 at 00:36