/java

How to Debug java with VS Code

Primary LanguageHTMLCreative Commons Zero v1.0 UniversalCC0-1.0

How to Debug Java with VS Code

Summary

Basic

Spec

  • OS _ ✅ MacOS _ ✅ Windows * ? Linux
  • Break Point _ ✅ break point _ ✅ condition break point * ❌ function breakpoint
  • Step Execution _ ✅ Step Over _ ✅ Step Into _ ✅ Step Out _ ✅ Continue _ ❌ Step Back _ ❌ Move To * ❌ Pause
  • Variables _ ✅ variables views _ ✅ watch variables
  • Call Stack * ✅ call stack
  • Evaluation _ ✅ eval expression to show variables _ ✅ eval expression to change variables
  • Type of Execution _ ✅ debug unit test _ ✅ debug executable package * ✅ remote debugging

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

  • Install OpenJDK or OracleJDK
  • set JDK path to environment value JAVA_HOME
  • set JDK/bin path to PATH

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