tags:

views:

200

answers:

2
+4  Q: 

Clojure load files

I'm trying to set up a simple clojure project, and I'm not sure how to load files between the project. I'm sure that the answer is in the documentation, but I can't find a simple answer any where and I'm not sure where to look.

Essentially, my directory looks like this:

Clojure/
    clojure/
        clojure.jar
        other clojure files
    clojure-contrib/
        clojure-contrib.jar
        other contrib files
    project/
        main.clj
        utils.clj

And I want main.clj to be something like this:

(ns project.main
  (:require project.utils))
(greet)

and utils.clj to be something like this:

(ns project.utils)
(defn greet [] (println "Hello, World!"))

But that fails with:

Exception in thread "main" java.io.FileNotFoundException: Could not locate project/utils__init.class or project/utils.clj on classpath:  (main.clj:1)

When I attempt to run it. My classpath includes the top Clojure/ directory and both jars. I also tried putting the project/ directory in the classpath as well, with no luck.

How do you set up a simple clojure project?

+4  A: 

You don't mention what your environment is (i.e. Emacs/SLIME/Swank, vim/Vimclojure), so I'm going to assume you are trying to invoke it from the command line.

You need to have your Clojure/ project directory in the classpath:

java -cp path/to/clojure.jar:path/to/clojure-contrib.jar:path/to/Clojure ...

Make sure to check that paths are correct relative to current working directory. It needs to point to the root of your namespace (i.e. if running in Clojure/, the path is .).

In fact, your project layout Works On My Machine(tm), with the exception that I have use instead of require (but you should've got a different error anyway if you got to the point when Clojure could find all your files).

Alex B
Thanks. It turns out my issue was because I was running from within `project/`, so I needed to add `..` to the classpath. It's working now. Vim\VimClojure, by the way. :)
Nate
+3  A: 

This answer I posted to another question should hopefully give you an idea of how your filenames should relate to namespace names for things to work. However, since your question is "how to set up a simple Clojure project", the following is a better start:

  1. Go to GitHub and grab Leiningen.

  2. Follow the instructions in the README. You'll end up doing something like

    $ lein new my-project
    $ cd my-project
    # ... edit project.clj ...
    $ lein deps
    
  3. Hack away! You'll need to put your files in the correct places. That will mean putting your source files in the directory tree rooted at my-project/src, with your core namespace most likely residing at my-project/src/my_project/core.clj. But really, I've explained all the details in the answer linked to above, so please read it (and do leave a comment if I missed something). :-)

Leiningen will take care of the basic project layout and setting up the classpath for a REPL / swank / nailgun for you (if you haven't yet come across the latter two, you will soon -- but that's a separate topic, the swank part of which I have covered to a certain degree e.g. in this SO answer), so hopefully you'll never need to deal with the java -cp ... nonsense by hand. (The swank-related answer I linked to in the last parenthetical remark has details on how to set up swank with the correct classpath from within Emacs.)

Michał Marczyk
Maybe it'll useful for questions' author: http://alexott.net/en/clojure/ClojureLein.html - an Introduction to work with Leiningen
Alex Ott