10

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 topClojure/ directory and both jars. I also tried putting theproject/ directory in the classpath as well, with no luck.

How do you set up a simple clojure project?

askedMay 26, 2010 at 14:38
So8res's user avatar

2 Answers2

9

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 yourClojure/ 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 inClojure/, the path is.).

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

answeredMay 26, 2010 at 14:59
Alex B's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It turns out my issue was because I was running from withinproject/, so I needed to add.. to the classpath. It's working now. Vim\VimClojure, by the way. :)
5

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 grabLeiningen.

  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 atmy-project/src, with your core namespace most likely residing atmy-project/src/my_project/core.clj. But really, I've explained all the details inthe 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 thejava -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.)

answeredMay 26, 2010 at 15:04
Michał Marczyk's user avatar

1 Comment

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

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.