Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure load files

Tags:

clojure

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?

like image 827
So8res Avatar asked May 26 '10 14:05

So8res


People also ask

How do I read a clojure file?

Reading the Contents of a File One Line at a Time If you want to get the entire contents of the file as a string one line at a time, you can use the clojure.java.io/reader method. The clojure.java.io/reader class creates a reader buffer, which is used to read each line of the file.

What is Clojure REPL?

A Clojure REPL (standing for Read-Eval-Print Loop) is a programming environment which enables the programmer to interact with a running Clojure program and modify it, by evaluating one code expression at a time.

How do I start Clojure REPL?

Starting a REPL Session To start a REPL session in Eclipse, click the Menu option, go to Run As → Clojure Application. This will start a new REPL session in a separate window along with the console output.


2 Answers

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

like image 70
Alex B Avatar answered Sep 24 '22 20:09

Alex B


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

like image 39
Michał Marczyk Avatar answered Sep 26 '22 20:09

Michał Marczyk