Clojure-JavaFX Application

At the beginning, it is difficult to get everything up and running with Clojure and JavaFX, specially starting the JavaFX thread, you may be familiar with the not very welcomed message “Toolkit not initialized”.

After few try-error configurations, I have reached a stable one.
This configuration allows the compilation and execution of the application using IntelliJ+Cursive and also the execution of the standalone uberjar generated by Leiningen doing just a double click on the jar.

In order to achieve this goal, we need to give the following structure to the core.clj and project.clj

;core.clj
(ns your-project.core
   (:gen-class :extends javafx.application.Application)
   (:require ...
             [your-project.view.mainwindow :as main-window]
             ...)
   (:import [javafx.application Application Platform]))


(defn -start [this stage]
   ...
   (main-window/construct-view stage)
   ...)


(defn -main [& args]
   (Application/launch (Class/forName "your-project.core") (into-array String [])))

Since core.clj extends Application, two functions must be implemented. -start, -main.
The stage can be passed to other namespaces.

;project.clj
(defproject your-project "0.2.1"
   ...
   :main your-project.core
   :profiles {:uberjar {:aot :all}}
   :repl-options {:init (do (compile 'your-project.core)
                            (future (-main)))})

It is necessary to indicate the main class, in this case, your-project.core
For the REPL options we make sure we compile the main class.
The future launches the application and does not block the REPL so you can interact with the running app.