274

Is it possible to attach the IntelliJ IDEA debugger to a running Java process? If yes, how?

SOLO
  • 868
  • 9
  • 19
Marco
  • 3,337
  • 2
  • 19
  • 25
  • 3
    Of course. You need to create a "Remote" Run/Debug configuration. More [here](http://www.jetbrains.com/idea/webhelp/run-debug-configuration-remote.html) – Vic Jan 14 '14 at 14:54
  • 2
    Um, these command line arguments are READ ONLY as of 14.0.2. You can edit only a couple of them, which doesn't help solve the problem in this question. Any idea how to fix this? – marknuzz Jan 28 '15 at 06:04
  • Answer is here: https://stackoverflow.com/questions/51207430/intellij-remote-debug-unable-to-open-debugger-port/52717704#52717704 – RoutesMaps.com Oct 09 '18 at 20:45
  • See also [Debug a java application without starting the JVM with debug arguments](https://stackoverflow.com/questions/376201/debug-a-java-application-without-starting-the-jvm-with-debug-arguments) – Vadzim Feb 28 '20 at 22:42

5 Answers5

409

Yes! Here is how you set it up.

Run Configuration

Create a Remote run configuration:

  1. Run -> Edit Configurations...
  2. Click the "+" in the upper left
  3. Select the "Remote" option in the left-most pane
  4. Choose a name (I named mine "remote-debugging")
  5. Click "OK" to save:

enter image description here

JVM Options

The configuration above provides three read-only fields. These are options that tell the JVM to open up port 5005 for remote debugging when running your application. Add the appropriate one to the JVM options of the application you are debugging. One way you might do this would be like so:

export JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"

But it depends on how your run your application. If you're not sure which of the three applies to you, start with the first and go down the list until you find the one that works.

You can change suspend=n to suspend=y to force your application to wait until you connect with IntelliJ before it starts up. This is helpful if the breakpoint you want to hit occurs on application startup.

Debug

Start your application as you would normally, then in IntelliJ select the new configuration and hit 'Debug'.

enter image description here

IntelliJ will connect to the JVM and initiate remote debugging.

You can now debug the application by adding breakpoints to your code where desired. The output of the application will still appear wherever it did before, but your breakpoints will hit in IntelliJ.

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • 3
    The "Remote" option is absent in my Intellij IDEA 15.0.2 on mac os el capitan. How can I get that option? – user674669 Mar 18 '16 at 23:38
  • @user674669 I'm not sure. Maybe `Remote` is not available on the community edition? I'm using the paid version. Can anybody with the community edition confirm? – Cory Klein Mar 28 '16 at 20:03
  • @CoryKlein Do you know how to grab the output from `System.out.println` in IntelliJ when remote debugging? – Don Rhummy Apr 07 '16 at 17:05
  • 1
    @DonRhummy If you figure out how, I'd be happy to add that information to this answer, but AFAIK IntelliJ does not currently have support for piping stdout and stderr from a remote application back to IntelliJ. – Cory Klein Apr 07 '16 at 18:04
  • If the remote server is accessible only over ssh how do we specifiy the key file? – vach Jun 29 '16 at 10:28
  • @vach open an ssh tunnel to the port on remote server and setup remote debugging onto the local (forwarded) port. You can use putty on windows. – bahti Dec 06 '16 at 11:10
  • The remote option was absent for me too, which brought me here ... the fix was simply to restart IDEA – wakjah Mar 01 '19 at 11:17
49

It's possible, but you have to add some JVM flags when you start your application.

You have to add remote debug configuration: Edit configuration -> Remote.

Then you'lll find in displayed dialog window parametrs that you have to add to program execution, like:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Then when your application is launched you can attach your debugger. If you want your application to wait until debugger is connected just change suspend flag to y (suspend=y)

Jakub Kubrynski
  • 13,724
  • 6
  • 60
  • 85
  • It confused me that you edit the inputs below and these then update the option strings above (in 13.1 CE, at least.) – Carl G May 28 '14 at 17:08
  • 8
    Um, these command line arguments are READ ONLY as of 14.0.2. You can edit a couple of them. Any idea how to fix this? – marknuzz Jan 28 '15 at 06:02
  • 1
    Also, what process are you attaching to by setting those options? – marknuzz Jan 28 '15 at 06:05
  • 5
    First one worked for me. Note to others - I needed to specify this before the -jar flag. – jim Dec 02 '15 at 22:56
  • 3
    @Nuzzolilo , the command line arguments displayed in idea are for display purposes only and hence they are read-only. They are not supposed to be added to idea but the remote java application which you want to debug. – Ganesh Krishnan Sep 12 '17 at 12:24
8

in AndroidStudio or idea

  1. Config the application will be debug, open Edit Configurations

add "VM Options" Config “-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005” remember "address"

enter image description here

  1. Config Remote Debugger if not exits, Click + to add

specify "Port" same as in Step 1 "address" enter image description here

Bill
  • 1,268
  • 14
  • 14
7

Also, don't forget you need to add "-Xdebug" flag in app JAVA_OPTS if you want connect in debug mode.

Max
  • 4,292
  • 1
  • 20
  • 14
0

Also I use Tomcat GUI app (in my case: C:\tomcat\bin\Tomcat9w.bin).

  • Go to Java tab:

    enter image description here

  • Set your Java properties, for example:

    Java virtual machine

    C:\Program Files\Java\jre-10.0.2\bin\server\jvm.dll

    Java virtual machine

    C:\tomcat\bin\bootstrap.jar;C:\tomcat\bin\tomcat-juli.jar

    Java Options:

    -Dcatalina.home=C:\tomcat

    -Dcatalina.base=C:\tomcat

    -Djava.io.tmpdir=C:\tomcat\temp

    -Djava.util.logging.config.file=C:\tomcat\conf\logging.properties

    -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000

    Java 9 options:

    --add-opens=java.base/java.lang=ALL-UNNAMED

    --add-opens=java.base/java.io=ALL-UNNAMED

    --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED

Taras Melnyk
  • 3,057
  • 3
  • 38
  • 34