Search code examples
clojure

ClassNotFoundException when trying to use class created using gen-class


My code:

(ns model.document
  (:gen-class
    :name model.document
    :implements java.io.Serializable
    :state "state"
    :init "init"
    :constructors {[String String String] []}
    :methods [[getContent [] String]
              [getTitle [] String]
              [getUrl [] String]]))

(defn -init [content title url]
  [[] (atom {:content content
             :title title
             :url url})])

(defn- get-field [this k]
  (@(.state this) k))

(defn getContent [this]
  (get-field this :content))

(defn getTitle [this]
  (get-field this :title))

(defn getUrl [this]
  (get-field this :url))

And it's use:

(ns classification-server.classifier
  (:require [model.document :refer :all]))

(new model.document "my-content" "my-title" "my-url")

And I get the unhelpful:

Caused by: java.lang.ClassNotFoundException: model.document, compiling:(classification_server/classifier.clj:13:12)

Please help me SO. You're my only hope...


Solution

  • The gen-class namespace you posted doesn’t compile, because the :implements specification expects a vector of symbols, not a symbol. If you change that line to

    :implements [java.io.Serializable]
    

    you will be able to compile (and instantiate) the class – however, it will not be functional, because there are other issues with your gen-class spec,such as the prefixed functions being absent (-getContent etc.).

    I suggest you read the gen-class documentation and simplify the problem further.