Sergey
Sergey

Reputation: 11908

Creating clojure library(jar) in order to call it from java

(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?

Upvotes: 3

Views: 242

Answers (2)

skuro
skuro

Reputation: 13514

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.

Upvotes: 2

Sergey
Sergey

Reputation: 11908

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.

Upvotes: 0

Related Questions