Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot load a new clojure library

I'm trying out clojure on my second day and I don't understand almost anything yet. I am working with the Programming Clojure 2nd ed. and I am stuck with libraries.

I have Leiningen and have the REPL running. The book first tells the reader to run a simple

(require 'clojure.java.io)

which works just fine (I get a nil). Then it wants to load a file called introduction.clj by running another simple

(require 'examples.introduction)

where I get an error message

FileNotFoundException Could not locate clojure/java/introduction__init.class
or clojure/java/introduction.clj on classpath:   clojure.lang.RT.load (RT.java:432)

I downloaded the introduction.clj file and looked where should I place it. The error and the book says the command will search in my classpath, but I have no idea where or what that is (after 1h of searching and reading I still don't get it, sorry). I ran a few commands and I had many classpaths listed (from which none contain a clojure/java/io.clj).

So I tried another approach - find the io.clj file on my disk and simply copy the file there and run it with a command

(require 'clojure.java.introduction)

This doesn't seem to work either. By the way, the io.clj file I found was in "C:\Program Files\clojure\src\clj\clojure\java". I tried running several other .clj files from the java folder as well from the clojure folder, like javadoc.clj or inspector.clj and all seem to work just fine with the above mentioned command. Only the new file doesn't seem to load this way.

Any help appreciated :)

like image 776
xificurC Avatar asked Sep 21 '12 14:09

xificurC


2 Answers

Are you reading the book "Programming Clojure"?

I have encountered the same problem. It ban be sovled as follows:

  1. If you start clojure by java: I work in windows, the clojure.jar is placed in D:\backup\clojure-1.5.1, and the source code of the book "Programming Clojure" is placed in D:\study\clojure\shcloj-code\code. You should first delete the user.clj file in folder D:\study\clojure\shcloj-code\code.

java -cp d:\backup\clojure-1.5.1\clojure-1.5.1.jar;d:\study\clojure\shcloj-code\code clojure.main -r

If you work in linux, replace the ";" with ":"

  1. If you start clojure by lein You should first cd to the D:\study\clojure\shcloj-code\code folder, and then

lein repl

You should also delete the user.clj file in folder D:\study\clojure\shcloj-code\code.

like image 140
tao qin Avatar answered Nov 11 '22 05:11

tao qin


Clojure runs on the Java Virtual Machine, so you will need to learn a bit about PATH and CLASSPATH concepts:

See: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Regarding the error message, the Clojure runtime is expecting to find introduction.clj in the directory clojure\java\example\introduction.clj (not where it really should be - see below).

The convention for Clojure namespaces is that the last component is the file name, while any previous components are parent directories. So

clojure.java.introduction

would have to be in the directory (relative to your source "root" or classpath)

clojure\java\introduction.clj

(The lein REPL automatically adds your source root to the classpath).

Another concept you need to understand is where the "root" of your source code is located. For Leiningen (the build tool you are using) the default is either "src" or "src/main/clojure" - as documented in the Leiningen sample project file on GitHub).

Finally, if you get really stuck, it seems the complete project for the book is available on GitHub.

Looking at the project, I see that you should actually be placing the file under src\examples\introduction.clj

like image 29
noahlz Avatar answered Nov 11 '22 03:11

noahlz