简体   繁体   English

创建clojure库(jar)以便从java调用它

[英]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". 我试图从java调用clojure函数,为此我创建了这个文件,它完全用lein编译,我通过调用“lein uberjar”创建一个jar文件。

Now the problem is that when I call it from java like this: 现在的问题是,当我从java这样调用它时:

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

it throws an ArityException 它抛出一个ArityException

Wrong number of args (2) passed to

I've tried to remove 'this' argument from clojure code but it didn't help. 我试图从clojure代码中删除'this'参数,但它没有帮助。 What's the problem with this code? 这段代码有什么问题?

You need to fix the :methods declaration. 您需要修复:methods声明。 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. 否则你只在编译的类中获得1-arity方法。

So the problem was in 所以问题在于

Java.util.HashMap

java should start from the small letter. java应该从小写字母开始。 But the bigger problem was that clojure lib didn't want to return java.util.HashMap as an object. 但更大的问题是clojure lib不想将java.util.HashMap作为对象返回。 The solution is to serialize it and then return. 解决方案是将其序列化然后返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM