Attach IntelliJ debugger at application startup

Normally when I want to debug a Java appliaction, I run the application and then connect the IntelliJ remote debugger to the JVM. Today I needed to debug the boot sequence of the application which meant I needed the debugger attached right from the start so as to catch the breakpoints at the beginning of the boot sequence, connecting manually would be too late.

In IntelliJ I changed the debugger mode from “attach” to “listen”. It then told me to use these command line arguments for the JVM:

-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=nick-laptop:5005,onthrow=,suspend=y,onuncaught=

What it doesn’t say is what values to use for the “onthrow” and “onuncaught” options. After a bit of fiddling I got it to work by setting “onuncaught=n” and removing entirely the “onthrow” clause:

-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=nick-laptop:5005,suspend=y,onuncaught=n

Linux users…

If you’re not using Linux then you can skip this bit, if you are, then this might be useful. I found it annoying to set IntelliJ to listen for incoming debug connections before starting the application, most of the time it’s fine tohave the JVM in listen mode and initiate connections as-and-when from IntelliJ. As a solution I added a couple of aliases to my ~/.bashrc file so that I could swap the behaviour easily:

# For IntelliJ in attach mode
export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
alias ijattach='export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"'

# For IntelliJ in listen mode
alias ijlisten='export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=nick-laptop:5005,suspend=y,onuncaught=n"'
This entry was posted in Java and tagged , , , . Bookmark the permalink.

2 Responses to Attach IntelliJ debugger at application startup

  1. Mark says:

    I was wondering about the above settings, can you clarify the address= setting?
    for example is nick-laptop:5005 your localhost? or machine name? and why port 5005?

    would the following also work for localhost? address=localhost:80

    I am trying to understand the meaning behind address=

  2. Nick says:

    Hi Mark – yes, the address setting is the address to the JVM that IntelliJ connects to in order to debug. “nick-laptop” is my machine’s hostname and port 5005 was the one provided by IntelliJ. I’m not entirely sure, but I presume that you can use any free port, so port 80 would work, providing of course you don’t have anything else using that port (e.g. Apache, etc).

Leave a Reply

Your email address will not be published. Required fields are marked *