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

后端 未结 5 1256
抹茶落季
抹茶落季 2020-12-05 03:13

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

java.io.FileNotFoundException: Could not locate hello__init.cla         


        
相关标签:
5条回答
  • 2020-12-05 03:50

    when I'm working from the repl and want to load files i find i have to call something like this first:

    (add-classpath "file:///home/arthur/.../src/")
    (add-classpath "file:///home/arthur/.../build/")
    

    before the repl can find them on the classpath. I put these in a file that is not included in the jar file along with a statement that reloads everything from the other files. When I build a jar file i find I don't need to do this.

    0 讨论(0)
  • 2020-12-05 03:51

    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.

    0 讨论(0)
  • 2020-12-05 04:02

    I've defined an alias (in .bash_profile) for loading the REPL:

    alias clojure='CLASSPATH=$HOME/git/clojure/clojure.jar:$HOME/git/clojure-contrib/clojure-contrib.jar:.:classes rlwrap java clojure.lang.Repl'
    
    0 讨论(0)
  • 2020-12-05 04:07

    Does hello.clj contain a (ns some-namespace) statement? If so, then same_namespace is appended to the each element of the CLASSPATH before looking for hello.clj

    0 讨论(0)
  • 2020-12-05 04:09

    I have the following lines on my .emacs:

    (setq swank-clojure-jar-path "~/src/clojure/clojure.jar"
          swank-clojure-extra-classpaths (append 
                          (directory-files "~/src/compojure/deps" t ".jar$")
                          (list
                           "~/src/swank-clojure/src/main/clojure"
                           "~/src/clojure-contrib/clojure-contrib.jar"
                           "~/src/clj/.")))
    

    I load the programe snake.clj which is at ~/src/clj and eval it (C-x h which selects the entire buffer and then C-c C-c to compile). The programs creates a namespace by name snake. Now, from emacs/slime, I do

    (use 'snake)
    

    That's it.Now invoke any function defined in the namespace.

    Your problem may be that the directory on which you have hello.clj may not be on your classpath. Also make sure you name the namespace properly. If inside hello.clj, you named your name space as hello, then you do (use 'hello).

    0 讨论(0)
提交回复
热议问题