ClassNotFoundException when importing a clojure interface

20 views
Skip to first unread message

Jack Park

unread,
Jul 17, 2021, 3:06:49 PMJul 17
to Clojure
I created a gist

which explains a ClassNotFoundException when I am importing and reifying a particular interface in another clj file.

It's really baffling because, in the load order, core calls a test in a test file - getting that to compile landed on the solution of an (:import ...) statement; once that worked, then the code in that test calls another file AndList.clj which happens to have several places where it reifies the same interface. Except, with the same import statement, on that file, I get the error.  Difficult to even find a decent StackOverflow because one such StackOverflow appears to be very similar, and the suggested fix is what I have now.

Thanks in advance for ideas.
Jack

Tanya Moldovan

unread,
Jul 17, 2021, 7:10:01 PMJul 17
to clo...@googlegroups.com
Hi,

I don't think it is the right way to use interfaces in clojure. Take a look at  this and this
You could create a java project with the interfaces you need and import that instead.

I think the issue is that this setup requires AOT and it might be missing from your configuration.
To fix it try adding this to project.clj file:
:profiles {:dev {:aot [ie4clj.api]}}
It can be tricky If you want to do lein uberjar and generate a jar file.

Alternatively, you can use compile-files (then you don't need import statement).
(note that in your gist you had some errors when defining AndList, I've fixed it)
(also take a look at the clojure style guide, as AndList is not really the way to name things in clojure )) )
(ns ie4clj.api)

(definterface Inferrable
  (^boolean eval [])
  (^boolean evalMembers [members]))
(ns ie4clj.AndList)

(when *compile-files*
  (require 'ie4clj.api))

(def AndList
  (reify
   ie4clj.api.Inferrable
   (eval [_] true)
   (evalMembers [_ m] true)))
Hope this helps,



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/ffb09a94-5aa4-4600-8c9a-e0d00901df72n%40googlegroups.com.

Jack Park

unread,
Jul 17, 2021, 8:44:18 PMJul 17
to clo...@googlegroups.com
Tanya,

That did help, swapping  ie4clj.api.Inferrable for its import.
That, of course, got me into the next coding bug, about which I shall ask next (I am attempting to use doseq to walk a list and AND its Inferrable members)

Meanwhile, I am truly surprised that you said

I don't think it is the right way to use interfaces in clojure.
It's likely a result of dyslexia that I did not see that coming after studying all the online banter about interfaces and protocols.. I chose definterface because the examples showed how to specify the return values, and programming by interface is how I do Java. I'd like to discover what, precisely, to read and get past dyslexic events to learn how to use interfaces.

Many thanks
Jack

Alex Miller

unread,
Jul 18, 2021, 3:34:45 PMJul 18
to Clojure
definterface is generally considered a low-level tool in Clojure and typically Clojure developers do not create interfaces directly like this.

The two primary means of function abstraction/polymorphism are multimethods (can dispatch on any aspect of any of the parameters to a function) and protocols (which dispatch on the type of the first parameter to the function). There are tradeoffs between these - the former is very flexible, the latter is very similar to interfaces (and does use them under the hood) so is limited to type-based dispatch but gets all the JVM performance of that operation.

Jack Park

unread,
Jul 18, 2021, 5:29:03 PMJul 18
to clo...@googlegroups.com
Thanks Alex!

I am beginning to move away from design by interface. Actually making progress now.

Cheers,
Jack

Reply all
Reply to author
Forward
0 new messages