Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile file in clojure

I've created file "hello.clj"

(ns clojure.examples.hello
    (:gen-class))

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

and try to compile

clojurec hello.clj

But I got this error

Exception in thread "main" java.io.FileNotFoundException: Could not locate hello/clj__init.class or hello/clj.clj on classpath: 
    at clojure.lang.RT.load(RT.java:398)
    at clojure.lang.RT.load(RT.java:367)
    at clojure.core$load__5058$fn__5061.invoke(core.clj:3734)
    at clojure.core$load__5058.doInvoke(core.clj:3733)
    at clojure.lang.RestFn.invoke(RestFn.java:413)
    at clojure.core$load_one__5010.invoke(core.clj:3578)
    at clojure.core$compile__5065$fn__5067.invoke(core.clj:3744)
    at clojure.core$compile__5065.invoke(core.clj:3743)
    at clojure.lang.Var.invoke(Var.java:346)
    at clojure.lang.Compile.main(Compile.java:56)

I try also to put this in the file and run clojore hello.clj

(compile 'clojure.examples.hello)

But got the same error.

like image 327
jcubic Avatar asked Jul 30 '10 08:07

jcubic


People also ask

What does clojure compile to?

The Clojure compiler produces Java byte code, which is typically then JIT-compiled to native code by the JVM.

What is AOT in Clojure?

Clojure compiles all code you load on-the-fly into JVM bytecode, but sometimes it is advantageous to compile ahead-of-time (AOT). Some reasons to use AOT compilation are: To deliver your application without source. To speed up application startup. To generate named classes for use by Java.

What does Gen class do in Clojure?

The generated class automatically defines all of the non-private methods of its superclasses/interfaces. This parameter can be used to specify the signatures of additional methods of the generated class.


1 Answers

A namespace called clojure.examples.hello needs to reside in a file called hello.clj in a directory $CPDIR/clojure/examples, where $CPDIR is a directory included in the JVM's classpath.

In general, trying to set the classpath and issue the compilation command by hand makes little sense. Use Leiningen instead; the README has a pretty thorough explanation of what you'll need to do.

like image 135
Michał Marczyk Avatar answered Sep 18 '22 09:09

Michał Marczyk