0

I'd like to do the following: I have a simple function written in Clojure/ClojureScript:

(defn add
  [a b]
  (+ a b))

I want to wrap this function into a Java class and put it in a jar so that I can access it in an existing Java/Android project. My previous approach was to use gen-class and create an uberjar. This however leads to some problems.

As an alternative approach I considered compiling the function using ClojureScript (a solution also suggested by Sam Beran).

So far I understand how to:

I'm not struggling to get the ClojureScript output into a format that can be passed on to the Rhino compiler.

Any thoughts?

IMPORTANT NOTE: I do not want to create a class with a main function as is done here!

Community
  • 1
  • 1
Gerrit Begher
  • 363
  • 2
  • 14

1 Answers1

1

One general way of going about this would be to:

At build time:

  • run the ClojureScript compiler to generate a JavaScript file
  • put the JavaScript file into the resource directory.
  • compile the Java class that uses it
  • make a jar file (an Uberjar or a normal Unterjar)

this Java class should:

  • make a java class that on initialization start up Rhino
  • on instantiation runs the javascript from the resource.

On a desktop of server you may not get the same awesome startup times My. Beran reported on android because processes on android have the advantage of starting life with a warmed up runtime with Rhino ready to go from the moment they start (they inherit it from the Zygote process)

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • 1
    Yes. But this approach yields a problem. From Sam Berans post: "[...], Startup speed was around 8-10 seconds - even worse than JVM Clojure! Some quick measurements indicated that the bulk of the time was spent with Rhino parsing the JS sources for cljs/core.js. I was pleased to discover that Rhino supports bytecode precompilation via the jsc utility [3]. I was able to precompile the ClojureScript output to bytecode, and achieve much faster startup - around 150ms on device. This is well within the target performance range, and is fast enough to eliminate any noticeable UI lag." – Gerrit Begher Jan 21 '15 at 07:52