Java Interoperability

Oracle Java

Clojure provides very clear and simple syntax for Java interoperatility, using the folowing functions

  • import - add functions from the Java library into the current namespace
  • new - create a new Java object
  • . - is the short form of the new function

    As Clojure is hosted on the Java Virtual Machine (JVM), its very easy to include libraries from any other languages that runs on the JVM, for example Java, Groovy, Scala, Jython, JRuby, Jaskell, etc.

    The Leiningen build tool provides a simple way to include libraries as dependencies, using the :dependencies section of the project.clj file. Any library published to Maven Central is available for download by Leiningen, as both Maven Central and Clojars.org repositories are used by default.

Hint Clojure projects and REPL environments include the java.lang library automatically. Any methods from that library can be used without having to import them or include any dependencies

The syntax

Its very easy to call Java methods and objects from clojure using the following syntax

  1. (.instanceMember instance args*)
  2. (.instanceMember Classname args*)
  3. (.-instanceField instance)
  4. (Classname/staticMethod args*)
  5. Classname/staticField

Note Use the instanceMember .toUpperCase to convert a string from lower case to upper case

Call the .toUpperCase function on any string you like, for example

  1. (.toUpperCase "I was low, but now I'm up")

The string passed as an arguent should now be all uppercase: “I WAS LOW, BUT NOW I’M UP”

Note Use the staticField Math/PI to return the approximate value of Pi

You can treat any static field like any name defined in your Clojure code, so when evaluated the static field simply returns the value it represents

In this case the Math/PI staic field simply returns the approximate value of Pi that fits into a java.lang.Double type.

  1. Math/PI
  2. -> 3.141592653589793

Getting the Java environment

Eariler we used Clojure functions to find information about our environment. We can also used the getProperty() method from the java.lang.System object to ask for the java version and jvm name.

Note Get version of Java & the JVM, returning those values as a meaningful string. Then get the version of the Clojure project

  1. (str "You are running Java version " (System/getProperty "java.version") "with the JVM" (System/getProperty "java.vm.name"))
  2. (str "Latest project version: " (System/getProperty "playground.version"))

Note Use System/getenv to return your system’s environment variables as a map

  1. (System/getenv)

Java Interoperability - 图2

You may notice that this is a map data structure that we return, so we can use use destructuring or the maps behaviour itself to pull out information.

Hint A full list of properties can be seen in the getProperty() documentation

There are more examples of Java Interoperability in the next few sections.