Question

I have following files:

src/my_proj/myns.clj:

(ns my-proj.myns)
(defrecord MyRecord  [a b c])

test/my_proj/myns_test.clj:

(ns my-proj.myns-test
  (:require [clojure.test :refer :all]
        [my-proj.myns :refer :all])
  (:import [my-proj.myns MyRecord]))

(def my-rec (MyRecord. "A" "B" "C"))

(deftest my-test
  (testing "test"
    (is (:a my-rec))))

When I run :

lein test

I get a ClassNotFoundException :

Exception in thread "main" java.lang.ClassNotFoundException: my-proj.myns.MyRecord
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at clojure.lang.DynamicClassLoader.findClass(DynamicClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.Class.forName0(Native Method)

What am I missing?

Was it helpful?

Solution

If you are importing, you'll need to change hyphens to underscores

user=> (ns sad-hyphen)
nil
sad-hyphen=> (defrecord MyRecord [a b c])
sad_hyphen.MyRecord
sad-hyphen=> (ns foo)
nil
foo=> (import '(sad-hyphen MyRecord))
ClassNotFoundException sad-hyphen.MyRecord ... 
foo=> (import '(sad_hyphen MyRecord))
sad_hyphen.MyRecord

However, there is not generally a need to import a record, just use the ->MyRecord and map->MyRecord constructors (brought into your namespace via require/refer).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top