Attach IntelliJ IDEA debugger to a running Java process

DebuggingIntellij Idea

Debugging Problem Overview


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

Debugging Solutions


Solution 1 - Debugging

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.

Solution 2 - Debugging

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)

Solution 3 - Debugging

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

Solution 4 - Debugging

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

Solution 5 - Debugging

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcoView Question on Stackoverflow
Solution 1 - DebuggingCory KleinView Answer on Stackoverflow
Solution 2 - DebuggingJakub KubrynskiView Answer on Stackoverflow
Solution 3 - DebuggingBillView Answer on Stackoverflow
Solution 4 - DebuggingMaxView Answer on Stackoverflow
Solution 5 - DebuggingTaras MelnykView Answer on Stackoverflow