2
votes

This is my myns/junk.clj file

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

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

I'm running lein ubserjar without issue. I've imported the generated jar in my java application, and then ran 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));   
    }
}

which produces error

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\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

What am I missing, and ideally, are there any kind of docs available which explain this stuff in detail?


Edit, per Alejandro's answer, I tried this

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

which now produces error

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\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)


What I wound up with was this

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

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

(-sq 7) ; works - 49

So it seems I need to declare the method as static, and define the actual method to have a leading dash in the name.

1

1 Answers

2
votes

You're missing something like this in your clojure file

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

Also, you'll need to define the method as static.

I think the canonical documentation is here: https://clojure.org/reference/compilation