2
  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

  • Do you have a classes directory in directory where you run the `java -cp` command? Try to find a file named clojure/examples/hello.class – DanLebrero Jan 21 '16 at 23:35
  • I have generated some project **tst** with leiningen, and there is a classes directory in: **tst/target/base+system+user+dave**. So i `cd` to the directory **base+system+user+dave** and from there i run `java -cp ./classes:clojure.jar tst.core Fred` and i get: `Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/IFn` Error, i try `java -cp ./classes:clojure.jar tst Fred` and i get `Error: Could not find or load main class tst` –  Jan 22 '16 at 00:57

1 Answers1

4

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

DanLebrero
  • 8,545
  • 1
  • 29
  • 30
  • Thanks for your detailed answer, but non of them still work for me! I run `java -cp ./classes:~/.m2/repository/org/clojure/clojure/1.7.0/clojure-1.7.0.jar tst.core` from **base+system+user+dave** dir and get: `Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/IFn` –  Jan 22 '16 at 02:21
  • You have to find the location of the Clojure jar being used on YOUR SYSTEM and plug it into the command. – Alex Jan 22 '16 at 15:02