Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Java maven spring-boot app in vs code?

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?

like image 343
Rodrigo5244 Avatar asked Apr 10 '17 11:04

Rodrigo5244


People also ask

How do I debug a Maven project in VS Code?

To debug Maven goals, right-click on a goal and start debugging. The Maven extension will call the Java debugger with the right parameters. This is a handy, time-saving feature.

How do I run a Spring Boot Maven project in VS Code?

Create the projectTo install, launch VS Code and from the Extensions view (Ctrl+Shift+X), search for vscode-spring-initializr . Once you have the extension installed, open the Command Palette (Ctrl+Shift+P) and type Spring Initializr to start generating a Maven or Gradle project and then follow the wizard.


2 Answers

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.

like image 53
leonhart Avatar answered Oct 07 '22 20:10

leonhart


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:

  • https://code.visualstudio.com/docs/java/java-spring-boot
  • How can I fix build failed, do you want to continue? in vscode
like image 34
Brent Bradburn Avatar answered Oct 07 '22 20:10

Brent Bradburn