DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java-Clojure Interop: Calling Clojure From Java

Java-Clojure Interop: Calling Clojure From Java

Mitch Pronschinske user avatar by
Mitch Pronschinske
·
Mar. 26, 10 · Java Zone · Interview
Like (0)
Save
Tweet
17.61K Views

Join the DZone community and get the full member experience.

Join For Free
Clojure is quickly becoming one of the more popular JVM languages because of its high performance and excellent handling of concurrency.  Rich Hickey's talk entitled "An Introduction For Java Programmers" is a great resource for Java programmers who are interested in learning Clojure.  For a basic overview of Clojure you can check out this Clojure Scripting tutorial, but one of the most sought after pieces of knowledge is how to interoperate [http://clojure.org/java_interop] between Java and Clojure.  There are several ways to interoperate including embedding Clojure in Java and calling Clojure from Java.  This short tutorial explains a method for calling Clojure from Java.

Some sites will recommend using clojure.lang.RT.  For this method, we assume that you've already built a .jar from your Clojure project and added it to the class path.  Calling Clojure from Java isn't accomplished simply by compiling to a .jar and calling the internal methods.  This example uses a simple Clojure file that can be compiled to a .jar (the example uses Clojure-1.1.0 .jar):
(ns com.domain.tiny
(:gen-class
:name com.domain.tiny
:methods [#^{:static true} [binomial [int int] double]]))

(defn binomial
"Calculate the binomial coefficient."
[n k]
(let [a (inc n)]
(loop [b 1
c 1]
(if (> b k)
c
(recur (inc b) (* (/ (- a b) b) c))))))

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

(defn -main []
(println (str "(binomial 5 3): " (binomial 5 3)))
(println (str "(binomial 10042 111): " (binomial 10042 111)))
)
When you run the .jar you get a result similar to this one:
(binomial 5 3): 10
(binomial 10042 111): 49068389575068144946633777...

This is a Java program that invokes the -binomial function in the tiny.jar:
import com.domain.tiny;

public class Main {

public static void main(String[] args) {
System.out.println("(binomial 5 3): " + tiny.binomial(5, 3));
System.out.println("(binomial 10042, 111): " + tiny.binomial(10042, 111));
}
}
And this is the result:
(binomial 5 3): 10.0
(binomial 10042, 111): 4.9068389575068143E263

Take note of the :methods keyword in the gen-class statement. This lets you access the Clojure function in a way similar to Java's static methods.  Then you just create a wrapper function that Java can call.  You can also see that the second version of -binomial has a dash in front of it.  

Because it is implemented as a Java class library, Clojure can also be easily embedded your Java applications, load code, and call functions.  Bob Martin of Object Mentor has another example of calling into Clojure from Java on his blog post: "Java Calling Clojure"
Java (programming language) Clojure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What's the Difference Between Static Class vs. Singleton Patterns in C#?
  • ETL/ELT on Kubernetes With Airbyte
  • Vaadin Apps as Native Executables Using Quarkus Native
  • Event-Driven Hello World Program

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo