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?

2 Answers 2

3

You need AOT compilation for gen-class.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it! I put the class in a separate file and called compile on that file, and it works.
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-")

3 Comments

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.
With or without lein, check that the class name matches the filename.clj. Just remember to replace '-' with '_'.
Also, please refer to the examples at clojuredocs.org/clojure_core/clojure.core/gen-class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.