Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

macos - Clojure can't find .clj in local directory, . and ./classes on CLASSPATH

When I evaluate (use 'hello) to load hello.clj, the REPL complains with the following error:

java.io.FileNotFoundException: Could not locate hello__init.class or hello.clj on classpath:  (NO_SOURCE_FILE:0)

I'm invoking the REPL like this:

java -cp "/Library/Java/Extensions/servlet-api-2.5-20081211.jar:... lots of jarfiles ...:/Library/Java/Extensions/clojure-contrib.jar:/Library/Java/Extensions/clojure-1.0.0.jar:./classes/:." jline.ConsoleRunner clojure.lang.Repl

Reading around, this looks like the file isn't being found in the PWD, but I've added . to the path with no success :-(.

Running with Java 1.6 on OS X 10.6.

I'm sure I'm being an idiot, can someone hit me with a LART?

EDIT: I also tried the ClojureX distro, and got the same results.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If hello.clj is in $DIR and $DIR is on your classpath, then hello.clj needs to start with (ns hello). If it's in $DIR/$SUBDIR and $DIR is on your classpath, then hello.clj needs to start with (ns $SUBDIR.hello). Generally, your filename structure and the ns name structure must match, with filename separator replaced by . in ns name and any _s in the filename corresponding to -s in the ns name. The name of the actual file needs to be the final component (possibly the only component, if the file's containing dir is on the classpath) of the namespace name.

EDIT:

An extended example. No information beyond what I've written above, so please skip over it if that was enough for you; however I know that getting CP right was quite painful for me in the beginning, so I decided I'd write this out in a step by step fashion so that someone, somewhere might perhaps be spared that particular `learning experience' ;-).

Say this is your namespace definition:

;;; in file "hello.clj"
(ns hello)

(defn hello []
  (println "Hello!"))

Then if you put the directory containing hello.clj on your classpath, you're good to go and a (use 'hello) at the REPL should do what you want.

If, on the other hand, you do this:

;;; in file "hello.clj"
(ns my-namespace)
;;; ...

or this:

;;; in file "my-filename.clj"
(ns hello)
;;; ...

-- that is, if you introduce a mismatch between the name of the file and the name of the namespace, Clojure won't be able to find your namespace.

Also, if you put hello.clj in /path/to/code, but what you have on your classpath is actually /path/to, i.e. the parent directory of /path/to/code, you need to do this:

;;; in file "/path/to/code/hello.clj"
(ns code.hello)
;;; ...

Then you'll be able to (use 'code.hello).

Finally, if you call your file my_namespace.clj, you need to call your ns my-namespace (and the other way around). _s in namespace names and -s in filenames should not be used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...