O'Reilly logo

Web Development with Clojure by Dmitri Sotnikov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required