Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

How To Cleanly Integrate Java and Clojure In The Same Package

A Hybrid Java/Clojure library designed to demonstrate how to setup Java interop using Maven.


This is a complete Maven-first Clojure/Java interop application. It details how to create a Maven application, enrich it with clojure code, call into clojure from Java, and hook up the entry points for both Java and Clojure within the same project.

Further, it contains my starter examples of using the fantastic Incanter Statistical and Graphics Computing Library in clojure. I include both a pom.xml and a project.clj showing how to pull in the dependencies.

The outcome is a consistent maven-archetyped project, wherein maven and leiningen play nicely together. This allows the best of both ways to be applied together. For the emacs user, I include support for cider and swank. NRepl by itself is present for general purpose use as well.

starting a project

maven first

create maven project

follow these steps

mvn archetype:generate -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

cd my-app

mvn package

java -cp target/my-app-1.0-SNAPSHOT.jar
# => Hello World

add clojure code

Create a clojure core file

mkdir -p src/main/clojure/com/mycompany/app

touch src/main/clojure/com/mycompany/app/core.clj

Give it some goodness…

  (:use (incanter core stats charts)))

(defn -main [& args]
  (println "Hello Clojure!")
  (println "Java main called clojure function with args: "
           (apply str (interpose " " args))))

(defn run []
  (view (histogram (sample-normal 1000))))

Notice that we’ve added in the Incanter Library and made a run function to pop up a histogram of sample data

add dependencies to your pom.xml

 <!-- pick your poison swank or cider. just make sure the version of nRepl matches. -->

java main class

Modify your java main to call your clojure main like in the following:


// for clojure's api
import clojure.lang.IFn;

// for my api
import clojure.lang.RT;

public class App
  public static void main( String[] args )

    System.out.println("Hello Java!" );

    try {

      // running my clojure code
      IFn main = RT.var("", "main");

      // running the clojure api
      IFn plus = Clojure.var("clojure.core", "+");
      System.out.println(plus.invoke(1, 2).toString());

    } catch(Exception e) {


maven plugins for building

You should add in these plugins to your pom.xml

add the maven-assembly-plugin

Create an Ubarjar

Bind the maven-assembly-plugin to the package phase this will create a jar file without the dependencies suitable for deployment to a container with deps present.


        <!-- use clojure main -->
        <!-- <mainClass></mainClass> -->

        <!-- use java main -->


add the clojure-maven-plugin

Add this plugin to give your project the mvn: clojure:… commands

A full list of these is posted later in this article.


add the maven-compiler-plugin

Add Java version targeting

This is always good to have if you are working against multiple versions of Java.


add the maven-exec-plugin

Add this plugin to give your project the mvn exec:… commands

The maven-exec-plugin is nice for running your project from the commandline, build scripts, or from inside an IDE.


add the maven-jar-plugin

With this plugin you can manipulate the manifest of your default package. In this case, I’m not adding a main, because I’m using the uberjar above with all the dependencies for that. However, I included this section for cases, where the use case is for a non-stand-alone assembly.


        <!-- use clojure main -->
        <!-- <mainClass></mainClass> -->

        <!-- use java main -->
        <!-- <mainClass></mainClass> -->


using maven


mvn package
run from cli with
run from java entry point:
java -cp target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
run from clojure entry point:
java -cp target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
run with entry point specified in uberjar MANIFEST.MF:
java -jar target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
run from maven-exec-plugin
with plugin specified entry point:
mvn exec:java
specify your own entry point:
java main
mvn exec:java -Dexec.mainClass=""
clojure main
mvn exec:java -Dexec.mainClass=""
feed args with this directive

run with maven-clojure-plugin
clojure main
mvn clojure:run
clojure test
add a test

In order to be consistent with the test location convention in maven, create a path and clojure test file like this:

mkdir src/test/clojure/com/mycompany/app

touch src/test/clojure/com/mycompany/app/core_test.clj

Add the following content:

  (:require [clojure.test :refer :all]
            [ :refer :all]))

(deftest a-test
  (testing "Rigourous Test :-)"
    (is (= 0 0))))
mvn clojure:test


mvn clojure:test-with-junit
available clojure commands

Here is the full set of options available from the clojure-maven-plugin:

mvn ...


see documentation:

add leiningen support

create project.clj

next to your pom.xml, create the clojure project file

touch project.clj

add this content

(defproject my-sandbox "1.0-SNAPSHOT"
 :description "My Encanter Project"
 :url ""
 :license {:name "Eclipse Public License"
           :url ""}
 :dependencies [[org.clojure/clojure "1.7.0"]
                [incanter "1.9.0"]]
 :source-paths ["src/main/clojure"]
 :java-source-paths ["src/main/java"]
 :test-paths ["src/test/clojure"]
 :resource-paths ["resources"]
 :aot :all)

note that we’ve set the source code and test paths for both java and clojure to match the maven-way of doing this

This gives us a consistent way of hooking the code from both lein and mvn. Additionally, I’ve added the incanter library here. The dependency should be expressed in the project file, because when we run nRepl from this directory, we want it to be available in our namespace, i.e.

run with leiningen

lein run

test with leiningen

lein test

running with org-babel

Make sure you jack-in to cider first:

M-x cider-jack-in (Have it mapped to F9 in my emacs)

clojure code

You can run these clojure blocks with C-c C-c in org-mode

Hello Clojure!
Java main called clojure function with args:

Note that we ran both our main and run functions here. -main prints out the text shown above. The run function actually opens the incanter java image viewer and shows us a picture of our graph.

I have purposefully not invested in styling these graphs in order to keep the code examples simple and focussed, however incanter make really beautiful output. Here’s a link to get you started:

playing with encanter

(use '(incanter core charts pdf))
;;; Create the x and y data:
(def x-data [0.0 1.0 2.0 3.0 4.0 5.0])
(def y-data [2.3 9.0 2.6 3.1 8.1 4.5])
(def xy-line (xy-plot x-data y-data))
(view xy-line)
(save-pdf xy-line "img/incanter-xy-line.pdf")
(save xy-line "img/incanter-xy-line.png")




Finally here are some resources to move you along the journey. I have drew on the links cited below along with a night of hacking to arrive a nice clean interop skeleton. Feel free to use my code available here:

For the eager, here is a link to my full pom:

org-babel clojure


project setup

working with Apache Storm (multilang)

starter project:

This incubator project from the Apache Foundation demos drinking from the twitter hose with twitter4j and fishing in the streams with Java, Clojure, Python, and Ruby. Very cool and very powerful..

Testing Storm Topologies in Clojure:


READ this to give your clojure workflow more flow

wrapping up

Clojure and Java are brothers of the JVM. They are easily mixed together allowing you to call between the languages with simple interop apis. For a more indepth example of writing consuming libraries written in Clojure inside your Java code, see Michael Richards’ article detailing how to use Clojure to implement interfaces defined in Java. He uses a FactoryMethod to abstract the mechanics of getting the implementation back into Java, which make’s the clojure code virtually invisible from an API perspective. Very nice. Here’s the link:

Happy hacking!..


A clean Java/Clojure interop project



No releases published


No packages published