Wednesday, May 29, 2013

Debugging a running Clojure Application

Clojure is fascinating.  Particularly for a programmer coming from a generation which is dominated by Object Oriented "Design" and C like syntax being more "readable". I was disturbed by the idea that I am unable to understand the Clojure code at first glance and I have to confess it really took couple of weeks to get my brain read Clojure code more fluently.

Coming from Java platform greatly spoiled by best of the developer tooling, Clojure development using Emacs is difficult (No offense to the tool as a tool is as good as its user). Let alone introducing the language to your friends in your work place is challenging, lack of "easy" IDE and debugging facilities would be very difficult to sell.  I am happy that I was wrong and my favorite IDE , IntelliJ IDEA has decent Clojure support and getting better every day.  One key task of any java developer is to debug the running application with less fuss and ...... Here is how they do it...

This setup consists of following :

  1. Run the Project using Leiningen in debug mode
  2. when required attach Intellij to the running application
  3. set the break points and fix/inspect the state of the application.
  4. Modify code and press Ctrl+Shift+F9 to hot replace the code and see changes immediately.
Let us look in to details:

Run the Project using Leiningen in debug mode

  • Add dev profile to your project.clj with the following jvm options

                   :jvm-opts ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5010"]
 
                Where 5010 is port number that is specified in Intellij Remote Debug Configuration
  • Add an alias to project.clj to be able to run project in debug mode using lein debug as follows  
             :aliases {   "debug" ["with-profile" "dev" "run"] }
  • whenever you run lein debug the standard run configuration will be executed with the jvm options which will allow you to attach debugger from IntelliJ Idea
When required attach Intellij to running application
  • Create a Remote Debug configuration to attach to the port specified in step above (5010 in example) and make it selected configuration and click debug when required
Other two steps are self explinatory.


Now Clojure development feels more natural and less demanding from tooling perspective given the great quality of  La Clojure plugin from JetBrains... which I know will keep getting better as every day passes.

I hope it would help budding Clojure enthusiasts in Java world.  


    

   

No comments: