Calling Out to Java

One advantage of using Clojure is that we can rely on the rich ecosystem of existing Java libraries. If some functionality is not available natively, we may wish to call out to a Java library to accomplish a particular task. Calling Java classes is very simple, and follows the standard Clojure syntax fairly closely.

Importing Classes

When we wish to use a Clojure library, we employ :use and :require statements. However, when we wish to import a Java class, we have to use the :import statement.

 
(​ns​ myns
 
(:import java.io.File))

We can also group multiple classes from the same package in a single import, as follows:

 
(​ns​ myns
 
(:import [java.io File FileInputStream FileOutputStream]))

Instantiating Classes

To create ...

Get Web Development with Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.