4

I'm trying to learn how to use gen-class in Clojure. I've started with this simple script:

(gen-class :name MyClass :prefix MyClass-)

(defn MyClass-toString[this] "This Is My Class")

(println (MyClass.))

When I try to run it I get

    Exception in thread "main" java.lang.IllegalArgumentException: Unable to resolve classname: MyClass

What am I doing wrong?

Idan Arye
  • 12,402
  • 5
  • 49
  • 68

2 Answers2

3

You need AOT compilation for gen-class.

kotarak
  • 17,099
  • 2
  • 49
  • 39
1

edit, Also, check that the main class name matches the one defined in the lein project file.

Usually you put in the (ns) header of the clj file.

(ns my.namespace
  (:gen-class))

Here's some examples

(gen-class
    :name "some.package.RefMap"
    :implements [java.util.Map]
    :state "state"
    :init "init"
    :constructors {[] []}
    :prefix "ref-map-")
runexec
  • 862
  • 4
  • 8
  • I'm trying to run this as a script with `clj`, without Leiningen, in the default namespace. I couldn't get `gen-class` to work in a lein project, so I'm trying to run the simplest thing possible - one class, no namespace, no state, no inheritance. And this has nothing to do with the main class I'm trying to run - because the main class is not named `MyClass` and I'm not trying to run a class named `MyClass`(only to construct it) - and that's the class the Exception is talking about. – Idan Arye Sep 26 '12 at 04:22
  • With or without lein, check that the class name matches the filename.clj. Just remember to replace '-' with '_'. – runexec Sep 26 '12 at 06:03
  • Also, please refer to the examples at http://clojuredocs.org/clojure_core/clojure.core/gen-class – runexec Sep 26 '12 at 06:09