Calling Clojure APIs from Java is discussed in my first tutorial. This tutorial explores calling your own Clojure functions from Java. A common use case for this is when you write a JavaFX UI and want to have the controller code written in Clojure.
Our first example explains how to call a simple Clojure function that takes no arguments and returns no values. (Technically, all Clojure fuctions return values, but that doesn’t mean we care about all values Clojure functions return.)
"Yeah. You guessed it. This prints 'Hello World!'"
(println "Hello World!"))
Continue reading “Calling Custom Clojure Functions from Java”
Mixing Clojure functions and Java methods usually happens when pure Clojure applications need to interact with Java libraries. However, sometimes you have Clojure functions receiving events from a Java program. This is often the case when you have a Java program with a JavaFX, Java Swing, or Java AWT GUI, but you want all the controller logic written in Clojure. It certainly is possible to write Clojure code calling Java libraries to create a decent UI, but doing so would be clunky at best.
It is also possible that you have a legacy Java application that you want to write Clojure plugins for. This is easily accomplished by calling Clojure functions from any appropriate place in the Java code. You can call directly into uncompiled Clojure scripts. To call Clojure scripts, you just need to know the right classes for loading your code.
Continue reading “Calling Clojure Functions from Java”