What is wrong with my simple Clojure gen-class script?

2.2k Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

You need AOT compilation for gen-class.

3
On

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-")