Calling Clojure from Java
Join the DZone community and get the full member experience.
Join For Free; printer.clj
(ns printer)
(defn print-string [arg]
(println arg))
// Java calling code
RT.loadResourceScript("printer.clj");
RT.var("printer", "print-string").invoke("hello world");
There's
a few things worth noting about the example: RT.var takes the namespace
name and the function name. The Var returned by RT.var has an invoke method that allows you to pass any number of Objects. The invoke method also returns an Object, which allows you return values from Clojure where necessary.
It's
also worth noting that the Clojure/Java interop is very, very good. You
could pass a Java object to Clojure, make changes to it in Clojure, and
return it back to Java. Of course, you might not even need to return it
to Java since the instance referenced in Clojure would be the same
instance referenced in Java.
Opinions expressed by DZone contributors are their own.
Trending
-
Never Use Credentials in a CI/CD Pipeline Again
-
5 Key Concepts for MQTT Broker in Sparkplug Specification
-
Introduction To Git
-
Authorization: Get It Done Right, Get It Done Early
Comments