Importing Java Classes in Clojure

June 7, 2017

Talking to Java

Clojure is hosted on the Java Virtual Machine (JVM) and as such can access Java libraries and classes quite easily. Some of Java is built in so to speak and included in Clojure. Look inside of the clojure.java.io namespace. A good example is file which returns a java.io.File object.

These and other examples are useful to understand. But, what if you have an external Java library which you'd like to use. Here is an example:

Apache Commons Lang

The commons lang library provides among other things a host of utilities for working with the Java API. Let's pick something within it that we'd like to use from within our Clojure program to demonstrate the process of working with an external Java library.

How about the class used in a recent post.

There I needed to unescape any Java literals from within a String. Specifically this function https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringEscapeUtils.html#unescapeJava-java.lang.String-

What are the steps to integrate this into your Clojure project and all the function. Let's work up from the function.

Step 1

The function is a static member function of a class within a library. Look at the home page of the library https://commons.apache.org/proper/commons-lang/. Here you'll find the maven information. This is what you'll need to know to add the library to your project. Midway down the page see the following block.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.5</version>
</dependency>

You can manually translate that to the vector needed by putting the groupID together with the artifactId and then put the version in a string. The result looks like:

[org.apache.commons/commons-lang3 "3.5"]

Now, if you don't trust this will work you can look up the library in Maven Central and there you'll get the same when you click on the Leiningen tab. Visit Maven Central and then search on commons-lang. You'll want the latest version. https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.5.

Notice the tabbed group. Inside the Leiningen tab you'll see the same string as above.

[org.apache.commons/commons-lang3 "3.5"]

Put this in your project.clj dependencies list.

Step 2

To use the class you'll need to import it. Here you'll want to go back to the API doc. To import you'll need the package and the class name.

If you navigate to https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html and click on the StringEscapeUtils link in the lower left panel you will then see the documentation for this class. The important information to note is the package. It is org.apache.commons.lang3.

With this you can create your import statement put the following inside your ns declaration at the top of your file.

    (:import [org.apache.commons.lang3 StringEscapeUtils])

Step 3

Calling the function is your last step. Since we are calling a static member function we simply use the classname followed by the member function separated by a slash. For this we do the following:

(StringEscapeUtils/unescapeJava "string to unescape is the parameter here")

Example Source

A complete example of the above is available in GitHub at the following url:

https://github.com/bradlucas/importing-java

Other resources


Tags: clojure java