Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run jar in debug mode from terminal

I'm using intellij idea IDE and I'm trying to run my jar file from terminal in debug mode and set breakpoints in a few places in the code.

the command I'm using is: java -jar myTestApp.jar -file "myfile.txt" -users myUser -Xdebug -Xrunjdwp:transport=dt_socket,server=127.0.0.1,suspend=n,address=8080

The problem is that I'm also using commons-cli library, so -Xdebug and -Xrunjdwp parameters are not recognized as Options, and I'm getting:enter image description here Any idea how to fix that?

like image 364
Pawel Avatar asked Oct 23 '18 11:10

Pawel


People also ask

How do I run a JAR file in debug mode in Linux?

Try using suspend=y , which makes the program halt immediately after initialization and wait for a debugger to connect. Also because you're using IntelliJ you can create a run configuration for an external jar, and start that in debug mode.

How do I run Java in debug mode?

A Java program can be debugged simply by right clicking on the Java editor class file from Package explorer. Select Debug As → Java Application or use the shortcut Alt + Shift + D, J instead. Either actions mentioned above creates a new Debug Launch Configuration and uses it to start the Java application.


3 Answers

Please assume the writer of the question is not using Java 5 in 2018:

java -agentlib:jdwp=transport=dt_socket,address=8080,server=y,suspend=n -jar myTestApp.jar -file "myfile.txt -users myUser

Btw: in case you use Java 9 and later: change address=8080 to address=*:8080 since localhost is no more the default.

stop telling people to use -Xdebug and -Xrunjdwp

Xdebug was used in Java 5 and below. Since Java 6 there is the -agentlib available. Xdebug allows access to the debugger over Xrunjdwp. 
JIT now starts in a compatibility-mode if you use Xdebug and uses a legacy-debugger which slows down your debugging extremely. 
People tell then to use -Djava.compiler=NONE to disable the compatibility-mode or to add -Xnoagent to disable the legacy debugger. Don't do that use -agentlib!

like image 143
SWiggels Avatar answered Oct 20 '22 03:10

SWiggels


Java expects only program arguments after specifying the class or jar to run. So simply try putting your JVM options before that:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=127.0.0.1,suspend=n,address=8080 -jar myTestApp.jar -file "myfile.txt" -users myUser 
like image 44
Damarus Avatar answered Oct 20 '22 04:10

Damarus


this worked for me

java -jar -Xdebug  -agentlib:jdwp="transport=dt_socket,server=y,suspend=n,address=5000" core-service-1.0-SNAPSHOT.jar
like image 25
Tabish Ahmed Avatar answered Oct 20 '22 02:10

Tabish Ahmed