How to Debug Java with VS Code

Summary

Basic

Spec

unit test (Junit)

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "Test Debug (Launch)",
      "request": "launch",
      "mainClass": "junit.textui.TestRunner",
      "args": "com.j74th.vscodedebugbook.bubblesort.BubbleSortTest"
    }
  ]
}

Instruction

executable file debug

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "Debug (Launch)",
      "request": "launch",
      "mainClass": "com.j74th.vscodedebugbook.bubblesort.BubbleSorter",
      "args": "4 3 2 1"
    }
  ]
}

attach running and remote process

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "Debug (Attach)",
      "request": "attach",
      "hostName": "192.168.1.24",
      "port": 5005
    }
  ]
}

how-to

  1. run program with debug option
    • set suspend=y when you like to stop before attach, otherwise suspend=n when you like to start right away.
java -cp target/classes -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=5005,suspend=y com.j74th.vscodedebugbook.bubblesort.BubbleSorter 4 3 2 1
  1. start debug