Debugging in Maven?

Is it possible to run a debugger like jdb from Maven? I have a pom.xml file that will successfully compile a project. However, the program freezes somewhere, and I would really like to run jdb or the equivalent debugger to see what happens.

I compile with mvn compile and run with:

 mvn exec:java -Dexec.mainClass="com.mycompany.app.App" 

I was expecting something like:

 mvn exec:jdb -Dexec.mainClass="com.mycompany.app.App" 

to run the debugger, but, as usual, my expectations are incompatible with the maven philosophy.

Also, I could not find the documentation (on the Maven or google website) to describe how debugging works. I suspect that I need to use some kind of plugin.

+62
java debugging maven-2
May 29 '10 at 14:12
source share
8 answers

As Brian said, you can use remote debugging:

 mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 com.mycompany.app.App" 

Then in your eclipse, you can use remote debugging and attach the debugger to localhost: 1044.

+32
May 29 '10 at 2:41 p.m.
source share

If you are using Maven 2.0.8+, run the mvnDebug command instead of mvn and attach the debugger to port 8000.

For Maven <2.0.8, uncomment the following line in %M2_HOME%/bin/mvn.bat (and possibly save the modified version as mvnDebug.bat ):

 @REM set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 

Read more in MNG-2105 and Working with the Eclipse-based IDE .

+100
May 29 '10 at
source share

I thought that I would expand these answers for OSX and Linux people (not that they need it):

I also prefer using mvnDebug . But after OSX Maverick destroyed my Java development environment, I started from scratch and came across this post, and thought I'd add to it.

 $ mvnDebug vertx:runMod -bash: mvnDebug: command not found 

Doh! I did not install it in this field after a new SSD and / or reset all Java when I installed Maverick.

I use the package manager for OSX and Linux, so I have no idea where mvn actually lives. (I know for short periods of time .. thanks, brew. I like that I don't know that.)

Let's watch:

 $ which mvn /usr/local/bin/mvn 

There you are ... little b @stard.

Now where did you install:

 $ ls -l /usr/local/bin/mvn lrwxr-xr-x 1 root wheel 39 Oct 31 13:00 / /usr/local/bin/mvn -> /usr/local/Cellar/maven30/3.0.5/bin/mvn 

Yeah! So you installed in /usr/local/Cellar/maven30/3.0.5/bin/mvn. You're a cheeky little build tool. Homegrown no doubt ...

Do you have your little mvnDebug buddy with you?

 $ ls /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug 

Good. Good. Fine. Everything goes according to plan.

Now move this little b @ star to where I can remember it easier.

 $ ln -s /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug /usr/local/bin/mvnDebug ln: /usr/local/bin/mvnDebug: Permission denied 

Damn it, computer ... You will obey my will. You know who I am? I A COURT! ONION!

 $ sudo ln -s /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug /usr/local/bin/mvnDebug 

Now I can use it from Eclipse (but why should I do this if I have IntelliJ !!!!)

 $ mvnDebug vertx:runMod Preparing to Execute Maven in Debug Mode Listening for transport dt_socket at address: 8000 

Internally mvnDebug uses this:

 MAVEN_DEBUG_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE \ -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" 

So you can change it (I usually debug on port 9090).

This blog post explains how to set up remote Eclipse debugging (shudder)

http://javarevisited.blogspot.com/2011/02/how-to-setup-remote-debugging-in.html

Same netbeans

https://blogs.oracle.com/atishay/entry/use_netbeans_to_debug_a

Same IntelliJ http://www.jetbrains.com/idea/webhelp/run-debug-configuration-remote.html

Here are some good documents on the -Xdebug command in general.

http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html

"-Xdebug provides debugging capabilities in the JVM that are used by the Java Virtual Machine Tool Interface (JVMTI). JVMTI is the low-level debugging interface used by debuggers and profiling tools. With it, you can check the status and monitor the execution of applications running in the JVM."

"The JVMTI subset that is most often used by profilers is always available. However, the functionality used by debuggers to enable step-by-step code execution and setting breakpoints has some overhead and is not always available. You must use the -Xdebug option to enable this function."

 -Xrunjdwp:transport=dt_socket,server=y,suspend=n myApp 

Check the docs on -Xrunjdwp too. You can enable it only when, for example, a specific exception is thrown. You can start it paused or running. Anyway .. I was distracted.

+21
Nov 14 '13 at
source share

I found an easy way to do this -

Just enter a command like this -

 >mvn -Dtest=TestClassName#methodname -Dmaven.surefire.debug test 

It will start listening on port 5005. Now just create remote debugging in Eclipse via Debug Configurations for localhost (any host) and port 5005.

Source - https://doc.nuxeo.com/display/CORG/How+to+Debug+a+Test+Run+with+Maven

+13
Aug 05 '15 at 7:11
source share

If you are using Netbeans, there is a good shortcut for this. Just define the target exec:java and add the jpda.listen=maven property Netbeans screenshot

Tested on Netbeans 7.3

+8
Jun 11 '13 at 14:01
source share

If you do not want to be dependent on the IDE and want to work directly with the command line, you can use 'jdb' (Java Debugger)

As mentioned by Samuel with a slight modification (set suspend = y instead of suspend = n, y means yes, which pauses the program and does not start it, so that you can set breakpoints for debugging it, if suspend = n means that it can run the program before completion before you can even debug it)

In the directory that contains your pom.xml, do:

 mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044 com.mycompany.app.App" 

Then open a new terminal and do:

 jdb -attach 1044 

Then you can use jdb to debug your program! =)

Sources: Java jdb Remote Debug Command Line Tool

+4
Feb 09 '16 at 21:22
source share

Why not use JPDA and connect to a running process from a separate debugger process? You should be able to specify the appropriate options in Maven to start your process with debug hooks enabled. This article has more information.

+3
May 29 '10 at 14:19
source share

I use the MAVEN_OPTS parameter and find it useful to set suspend to "suspend = y" as my exec: java programs tend to be small generators that are finished before I can connect the debugger .... :) With a pause, the debugger will wait on it to be attached before the procedure.

+3
Jul 23 2018-10-23T00:
source share



All Articles