18

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?

Pawel
  • 417
  • 1
  • 6
  • 25

4 Answers4

44

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!

SWiggels
  • 2,159
  • 1
  • 21
  • 35
4

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 
Damarus
  • 65
  • 3
  • Thasks, that worked ( its not throwing an error) but the execution still doesn't stop at breakpoint. What am I doing wrong? – Pawel Oct 23 '18 at 11:33
  • 2
    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. – Damarus Oct 23 '18 at 11:39
  • 1
    downvoted since this answer affect the performance badly and is deprecated since years. See my answer for an up to date solution. – SWiggels Jan 03 '19 at 17:06
  • I used the exact arguments of the original question, it's not my specific advice. – Damarus Jan 07 '19 at 15:09
  • @Damarus I understand this. But you are spreading, even if its not on purpose, bad information. I'm sorry but your answer should not have a high ranking because of outdated information. – SWiggels Jan 17 '19 at 08:32
1

-Xdebug should be moved in front of the -jar parameter. Now Java thinks it's part of your program's arguments.

SurfMan
  • 1,685
  • 12
  • 19
0

this worked for me

java -jar -Xdebug  -agentlib:jdwp="transport=dt_socket,server=y,suspend=n,address=5000" core-service-1.0-SNAPSHOT.jar
  • I needed to run this jar on a server and debug the code on my local machine in eclipse as Remote java application. So I used this on the server to start the jar – Tabish Ahmed Mar 03 '20 at 09:32
  • So I am trying to run a jar file in another device and test it from my local machine, it should be possible right? If so how does the connection looks like in eclipse? – Fay007 Jul 14 '22 at 15:23