Java 从终端以调试模式运行 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52947783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:40:39  来源:igfitidea点击:

run jar in debug mode from terminal

javaapache-commons-cli

提问by Pawel

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.

我正在使用 intellij idea IDE,我试图在调试模式下从终端运行我的 jar 文件,并在代码中的几个地方设置断点。

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

我使用的命令是: 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 hereAny idea how to fix that?

问题是我也在使用 commons-cli 库,所以 -Xdebug 和 -Xrunjdwp 参数不被识别为选项,我得到:在此处输入图片说明知道如何解决这个问题吗?

采纳答案by SWiggels

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

请假设问题的作者在 2018 年没有使用 Java 5:

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

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=8080to address=*:8080since localhost is no more the default.

顺便说一句:如果您使用Java 9 及更高版本:更改address=8080address=*:8080因为 localhost 不再是默认值。

stop telling people to use -Xdebugand -Xrunjdwp

停止告诉人们使用-Xdebug-Xrunjdwp

Xdebugwas used in Java 5 and below. Since Java 6 there is the -agentlibavailable. Xdebugallows access to the debugger over Xrunjdwp. ?JIT now starts in a compatibility-mode if you use Xdebugand uses a legacy-debugger which slows down your debugging extremely. ?People tell then to use -Djava.compiler=NONEto disable the compatibility-mode or to add -Xnoagentto disable the legacy debugger. Don't do that use -agentlib!

Xdebug用于 Java 5 及以下版本。从 Java 6 开始就有了-agentlibXdebug允许通过 访问调试器Xrunjdwp。?JIT 现在以兼容模式启动,如果您使用Xdebug并使用遗留调试器,这会极大地减慢您的调试速度。?人们告诉然后使用-Djava.compiler=NONE禁用兼容模式或添加-Xnoagent禁用遗留调试器。不要那样用-agentlib

回答by SurfMan

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

-Xdebug 应该移到 -jar 参数的前面。现在 Java 认为它是程序参数的一部分。

回答by Damarus

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

在指定要运行的类或 jar 之后,Java 只需要程序参数。因此,只需尝试在此之前放置您的 JVM 选项:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=127.0.0.1,suspend=n,address=8080 -jar myTestApp.jar -file "myfile.txt" -users myUser 

回答by Tabish Ahmed

this worked for me

这对我有用

java -jar -Xdebug  -agentlib:jdwp="transport=dt_socket,server=y,suspend=n,address=5000" core-service-1.0-SNAPSHOT.jar