cannot load a new clojure library
Asked Answered
S

2

10

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

Sonometer answered 21/9, 2012 at 14:2 Comment(1)
what is that examples.introduction ? a jar o ra clj file?Obliquity
E
6

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

Edulcorate answered 21/9, 2012 at 14:23 Comment(5)
Thanks for the reply, I read the document about the paths and classpaths and it helped a bit. The problem still remains though. I tried placing the file literally everywhere in the clojure's main folder (and in subfolders) and it simply doesn't work. 'examples.introduction doesn't work, 'clojure.examples.introduction doesn't work, to put it short I don't feel like this is the issue. Every other clj file loads correctly from the folders but when I put the introduction.clj file there it cannot locate it. Anywhere. Can there be any other reason for that? I really don't know what to do. ThanksSonometer
You also need (ns examples.introduction) at the top of the clj file. The namespace in the file generally needs to match the location in the file system. Unless you have ahead-of-time compiled the clojure source into a Java class and packaged it in a jar (you can get to that later).Edulcorate
Even that doesn't help. I even tried taking the introduction.clj file to the same folder where the io.clj file is. That one works with the command (require 'clojure.java.io) and it's first line says ns clojure.java.io. So I changed the introduction.clj file's first line to ns clojure.java.introduction. I mean that should work, right? Well, even that doesn't. Same folder, same path, but it doesn't work. I'm starting to think I skipped installing something, although Leiningen should take care of everything, no?Sonometer
Advice: start over from scratch, use the project on github as your guide. Good luck.Edulcorate
Thanks for the advices ;) I will try to install from scratch as you suggested. Off topic: Clojure seems fun!Sonometer
C
1

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.

Caisson answered 4/2, 2014 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.