Clojure :gen-class 在 Java 应用程序中不工作

Clojure :gen-class not working in Java app

这是我的 myns/junk.clj 文件

(ns myns.junk
  (:gen-class
    :name booklist.util
    :methods [[sq [int] int]]))

(defn sq [x] (* x x))

我 运行 lein ubserjar 没问题。我在我的 java 应用程序中导入了生成的 jar,然后 运行 this

package javaapplication1;

import booklist.util;

public class JavaApplication1 {
    public static void main(String[] args) {
        util u = new util();
        System.out.println(u.sq(45));   
    }
}

产生错误

Exception in thread "main" java.lang.UnsupportedOperationException: sq (myns.junk/-sq not defined?) at booklist.util.sq(Unknown Source) at javaapplication1.JavaApplication1.main(JavaApplication1.java:14) C:\Users\X750JA\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

我错过了什么,理想情况下,是否有任何类型的文档可以详细解释这些内容?


编辑,根据 Alejandro 的回答,我试过了

(ns myns.junk
  (:gen-class
    :name booklist.util
    :methods [[sq [int] int]]))

(defn sq [x] (* x x))

(defn -sq
  "A Java-callable wrapper around the 'sq' function."
  [n]
  (sq n))

现在会产生错误

Exception in thread "main" clojure.lang.ArityException: Wrong number of args (2) passed to: junk/-sq at clojure.lang.AFn.throwArity(AFn.java:429) at clojure.lang.AFn.invoke(AFn.java:36) at booklist.util.sq(Unknown Source) at javaapplication1.JavaApplication1.main(JavaApplication1.java:14) C:\Users\X750JA\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)


我最后得到的是这个

(ns myns.junk
  (:gen-class
    :name booklist.util
    :methods [#^{:static true} [sq [int] int]]))

(defn -sq [x] (* x x))

(-sq 7) ; works - 49

看来我需要将方法声明为静态方法,并定义名称中有前导破折号的实际方法。

您的 clojure 文件中缺少这样的内容

(defn -sq
  "A Java-callable wrapper around the 'sq' function."
  [n]
  (sq n))

此外,您需要将方法定义为静态方法。

我认为规范文档在这里:https://clojure.org/reference/compilation