3
votes
(ns lol.test
  (:gen-class
   :name lol.test
   :methods [[createHashMap [String] Java.util.HashMap]])
  (:import [java.util HashMap]))

(defn -createHashMap [this s]
  (HashMap. (assoc {} s "test")))

I'm trying to call clojure functions from java, for this purpose I've created this file which prefectly compiles with lein, I create a jar file by calling "lein uberjar".

Now the problem is that when I call it from java like this:

lol.test l = new lol.test();
l.createhashMap("test");

it throws an ArityException

Wrong number of args (2) passed to

I've tried to remove 'this' argument from clojure code but it didn't help. What's the problem with this code?

2
What is your return type? should it be java.util.HashMap? - Abimaran Kugathasan

2 Answers

2
votes

You need to fix the :methods declaration. Right now you have

:methods [[createHashMap [String] java.util.HashMap]]

It has to become

:methods [[createHashMap [Test String] java.util.HashMap]]

otherwise you're only getting a 1-arity method in your compiled class.

0
votes

So the problem was in

Java.util.HashMap

java should start from the small letter. But the bigger problem was that clojure lib didn't want to return java.util.HashMap as an object. The solution is to serialize it and then return.