DZone
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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Trending

  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • Agile and Quality Engineering: A Holistic Perspective
  1. DZone
  2. Coding
  3. Java
  4. Java Clojure Interop: Integrating Clojure into Your Java Project

Java Clojure Interop: Integrating Clojure into Your Java Project

By 
Mitch Pronschinske user avatar
Mitch Pronschinske
·
Mar. 10, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
18.6K Views

Join the DZone community and get the full member experience.

Join For Free
It's easier to wrap an existing piece of Java code in Clojure than it is to do the inverse, but because Clojure is implemented as a Java class library, it's also relatively simple to embed Clojure in your Java applications, load code, and call functions.  Repl.java and Script.java in the distribution are the normal examples for loading code from a user or from a file.  In this quick tutorial, you'll find out how to use the same underlying machinery to load Clojure code and then manipulate it directly from Java.

First, let's start with this script that defines a simple Clojure function:
; foo.clj
(ns user)

(defn foo [a b]
(str a " " b))
This Java class will load the script and call the of function with arguments in the form of Java objects.  Then it will print the returned object:
// Foo.java

import clojure.lang.RT;
import clojure.lang.Var;

public class Foo {
public static void main(String[] args) throws Exception {
// Load the Clojure script -- as a side effect this initializes the runtime.
RT.loadResourceScript("foo.clj");

// Get a reference to the foo function.
Var foo = RT.var("user", "foo");

// Call it!
Object result = foo.invoke("Hi", "there");
System.out.println(result);
}
}
Finally, you have to compile this and run it to see the printed result.  For compiling Clojure into a .jar, Leiningen is a favorable option since it is made specifically for Clojure.  

There is a good video that explains how to use leiningen.  It has the advantage of letting users write everything in Clojure, rather than writing a bunch of XML code like you would for Ant or Maven.  Leiningen also has "uberjars," which build in Clojure and put all of your Clojure dependencies into one standalone file, meaning less work for you.

If you want to be more Java friendly in your approach, you could add an Ant task to build it along with the Java project.  This will just take a little more work.  You'll need to call 'to-array' on the functions that need to return proper java arrays.  Clojure supports the creation, reading, and modification of Java arrays, but it is recommended that you limit use of arrays to interop with Java libraries that require them as arguments or use them as return values.

Once you have your .jar file, just add it to your java project as a build deployment.  then you can call it directly from Java.  Here is the printed result when you run the .jar:
>javac -cp clojure.jar Foo.java

>java -cp clojure.jar Foo
Hi there

>
If you're doing Java interop from Clojure, things are even more simple.  Clojure programs can use any Java class or interface. The classes in the java.lang package can be used in Clojure just like you would in Java without having to import them. Java classes in other packages are used by either specifying their package when referencing them or using the import function. Invoking Java methods from Clojure code is also pretty simple.  As a result, Clojure doesn't provide functions for many common operations.  Instead, it relies on Java methods.

[http://java.dzone.com/articles/java-clojure-interop-calling]
Java (programming language) Clojure

Opinions expressed by DZone contributors are their own.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!