How to inspect a legacy Java application with the Clojure REPL

Gert-Jan van de Streek

Gert-Jan van de Streek

Published: 18 May, 2015

When you get used to the REPL in Clojure it's hard to go back to the compile / redeploy round trip in Java. In very complex projects/products it's nice to have a REPL around where you can quickly try stuff. But wait, Clojure runs on the VM and the Java interop is great. Why not add the Clojure REPL to your Java project and take this as a start for adding Clojure modules!

Adding the REPL

Adding the REPL is as simple as requiring clojure.core and and call start-server on the nrepl-server. To do this from Java you need the clojure java api. When you... ok, ok, I'll just show you the code:

Connect to the REPL

Our code does not specify a port, so you need to find out what port the repl is using (use netstat -a | grep LISTEN or the likes). When you know the port, connect to the repl with leiningen:

Look around

In our case we have embedded the REPL inside JIRA, which enables us to look around and see what all the magic managers and services can do for us:

That's it, now you have a way to look around inside a Java process. That's a start. You are now ready to mix and match Java and Clojure for new development. What's keeping you?

Did you enjoy reading?

Share this blog with your audience!