20

I was able to debug a simple Java hello world. The first step was to "compile" with javac -g. I looked up how I would acomplish the same with maven and found http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html, but those instructions are for running the application and wait for a debugger to connect.

I also tried to use target/classes for classpath in launch.json. The debugger complains that it cannot find a file in the root directory /, but it runs. Althought the debugger is running, the application is not responding to HTTP requests.

Is there a mvn command to compile the application with javac -g and produce a .class the debugger is able to run successfully?

Rodrigo5244
  • 5,145
  • 2
  • 25
  • 36

2 Answers2

19

You will only be able to remote debug with vs code, so a simple command will be mvnDebug spring-boot:run, which will do the same thing as mvn spring-boot:run but add these options:

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y

Then you can attach from vs code, a sample launch.json looks like:

{
 "version": "0.2.0",
  "configurations": [

    {
      "type": "java",
      "name": "Debug (Launch)",
      "request": "launch",
      "mainClass": "",
      "args": ""
    },
    {
      "type": "java",
      "name": "Debug (Attach)",
      "request": "attach",
      "hostName": "localhost",
      "port": 8000
    }
  ] 
}

and you can select Debug(Attach) from the debug panel to run.

leonhart
  • 1,193
  • 6
  • 12
  • 1
    PS: If you are using Java 1.8+ use these flags instead `mvn spring-boot:run -Dspring-boot.run.jvmArguments="agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000"` as answered here: https://github.com/Microsoft/vscode-java-debug/issues/300 – Chandrahas Aroori Nov 22 '20 at 09:59
5

Assuming you've installed the collection package Java Extension Pack by Microsoft debugging Maven Spring Boot applications seems to work right-out-of-the-box.

Launch code from the project's root directory and "Start Debugging". There are multiple ways to launch the debbugger -- the most straightforward is to just hit F5 and, if it asks, select Java.

Under nominal conditions this triggers the following steps:

  1. Your application is compiled into class files
  2. It finds and launches your application's main function -- which should include SpringApplication.run()
  3. The application runs (with breakpoints enabled) and log output is sent to the TERMINAL panel.

Related links:

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173