Clojure.org documentation on compilation and gen-class

101 views Asked by At
  1. I am reading the documention on clojure.org about compilation, the last part gen-class examples. I do the examples and then when trying to run it as java app with: java -cp ./classes:clojure.jar clojure.examples.hello Fred in the terminal i get : Error: Could not find or load main class clojure.examples.hello. Can someone help?

  2. Can someone introduce where to learn about gen-class and :gen-class, i find not much documentation on web

1

There are 1 answers

2
DanLebrero On

The command java -cp ./classes:clojure.jar tst.core from your base+system+user+dave is almost correct. The java.lang.NoClassDefFoundError: clojure/lang/IFn error is because the JVM cannot find the Clojure classes as there is no clojure.jar file in the base+system+user+dave directory, so you need to specify the correct path for the clojure.jar file.

As you are using lein, it downloads your project dependencies to your local repository. One of the dependencies will be Clojure itself, so assuming you are on iOS/Linux and that your lein project.clj has a dependency with clojure 1.7.0, the command to run from the base+system+user+dave directory will be:

java -cp ./classes:~/.m2/repository/org/clojure/clojure/1.7.0/clojure-1.7.0.jar tst.core 

As this gets quite annoying once you have more than one dependency, I would suggest to use lein uberjar that will create a file in the target directory called your-project-name-standalone.jar that will have all required classes, so to run it from the command line go to the target directory and run something like :

java -cp tst-standalone.jar tst.core

To understand more about how the classpath works in the JVM, you can start with Wikipedia