A problem with compilation Clojure files

28 views
Skip to first unread message

TPJ

unread,
Sep 13, 2009, 11:58:40 PM9/13/09
to Clojure
I'm trying to compile a simple Clojure program, in order to get some
Java class
file. Here's my session (I'm in /home/tpj/test):

$ ls -R
.:
classes src

./classes:

./src:
clojure

./src/clojure:
examples

./src/clojure/examples:
hello.clj

$ runclojure -cp `pwd`/classes:`pwd`/src
Clojure 1.0.0-
user=> (System/getProperty "java.class.path")
"jline-0.9.94.jar:clojure-1.0.0.jar:/home/tpj/test/classes:/home/tpj/
test/src"
user=> (compile 'clojure.examples.hello)
java.io.IOException: No such file or directory (hello.clj:1)

$ cat src/clojure/examples/hello.clj
(ns clojure.example.hello
(:gen-class))

(defn -main [arg]
(println (str "Hello, " arg "!")))

What am I doing wrong? (I use Clojure 1-0-0, downloaded in the binary
form
from the main Clojure site., and Java 1.6.0_14.)

Richard Newman

unread,
Sep 14, 2009, 12:01:40 AM9/14/09
to clo...@googlegroups.com
> ./src/clojure/examples:
> hello.clj

...

> $ cat src/clojure/examples/hello.clj
> (ns clojure.example.hello
> (:gen-class))

Possibly it's just that your ns doesn't match up to the source path
(examples versus example).

TPJ

unread,
Sep 14, 2009, 12:00:23 PM9/14/09
to Clojure
On 14 Wrz, 06:01, Richard Newman <holyg...@gmail.com> wrote:
> (...)
> Possibly it's just that your ns doesn't match up to the source path  
> (examples versus example).

Nope, it's not that easy. I changed "clojure.example.hello" to
"clojure.examples.hello" in the hello.clj file, and the message was
still the same. (Hard to believe, isn't it?)

I spent all the Sunday trying to compile anything, without any
success. It's either some stupid mistake on my side (I believe so, and
I hope so!), or something... (I don't know, what to say.)

Michael Wood

unread,
Sep 14, 2009, 1:51:22 PM9/14/09
to clo...@googlegroups.com
2009/9/14 TPJ <tpr...@gmail.com>:

What does your "runclojure" script look like?

--
Michael Wood <esio...@gmail.com>

Richard Newman

unread,
Sep 14, 2009, 2:06:10 PM9/14/09
to clo...@googlegroups.com
> Nope, it's not that easy. I changed "clojure.example.hello" to
> "clojure.examples.hello" in the hello.clj file, and the message was
> still the same. (Hard to believe, isn't it?)

What's the value of *compile-path*? Is it in your classpath?

TPJ

unread,
Sep 15, 2009, 1:00:48 PM9/15/09
to Clojure
On 14 Wrz, 19:51, Michael Wood <esiot...@gmail.com> wrote:
> (...)
> What does your "runclojure" script look like?

Yes, that's the point. If the problem isn't in the Clojure itself, nor
in the source file, and the files are placed exactly where they should
be, the only possibility is the script used to run Clojure.

I'll explain it here.

First of all, I don't name my script "runclojure", I name it "clj".
I'm too lazy to type so many characters; I used the name "runclojure",
because it's more descriptive.

The script is composed of two parts.

The first part is responsible for handling the classpath given in the
arguments passed to the script. This classpath is saved in the
variable CP. (This variable starts either with the : character, or is
empty.)

Then, the second part of the script is executed. In the old version of
my script, this part was as follows:

(cd /path/to/my/clojure/dir
exec java -cp jline-0.9.94.jar:clojure-1.0.0.jar$CP \
jline.ConsoleRunner clojure.lang.Repl)

So, the JVM was run in my Clojure directory. Thus, the java classpath
returned by the getProperties method was simply:

"jline-0.9.94.jar:clojure-1.0.0.jar:
/home/tpj/test/classes:/home/tpj/test/src"

Please note, that both the jline and clojure jars are given without
any paths - they are placed in the current working directory.
Obviously, all my source files aren't - and it was the cause of my
problems.

I changed the second part of the script to:

BASECP="/path/to/my/clojure/dir"
exec java -cp $BASECP/jline-0.9.94.jar:$BASECP/clojure-1.0.0.jar$CP \
jline.ConsoleRunner clojure.lang.Repl

Now, the java classpath returned by the getProperty method is as
follows:

"/path/to/my/clojure/dir/jline-0.9.94.jar:
/path/to/my/clojure/dir/clojure-1.0.0.jar:
/home/tpj/test/classes:/home/tpj/test/src"

And the compilation works as expected.

Although I still don't understand why the current working directory
can cause such problems (shouldn't Java be able to make proper use of
the classpath?), my problem has been solved.

Thank you all for your answers, for your *other* point of view on this
problems, which helped me so much!

TPJ

unread,
Sep 15, 2009, 1:02:44 PM9/15/09
to Clojure
In both cases (the old, buggy version of my script, and the new one,
fixed) the value was the same: "classes". It's hard to say whether
it's
in the classpath, since no prefix is given (I can't tell you what the
full compile path is).

Richard Newman

unread,
Sep 15, 2009, 1:26:18 PM9/15/09
to clo...@googlegroups.com
> Although I still don't understand why the current working directory
> can cause such problems (shouldn't Java be able to make proper use of
> the classpath?), my problem has been solved.

Because the compilation of Clojure files produces files in *compile-
path* ("classes" by default), which is relative to the current working
directory on launch, and must be in your classpath.

If you were launching the JVM from your Clojure directory, you weren't
starting where you could see your "classes" directory. It would be
trying to use /path/to/my/clojure.dir/classes, which doesn't exist, or
isn't on your classpath. Error.

Either launch from /home/tpj/test/ (which seems to be what you're
doing now), or bind *compile-path* to "/home/tpj/test/classes".

Reply all
Reply to author
Forward
0 new messages