通过执行“gradle run”来调试 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33035922/
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
Debug Java program by executing "gradle run"
提问by Sky Dog
I have a Gradle project in Eclipse IDE and I usually use the option gradle run
to run my Java application.
我在 Eclipse IDE 中有一个 Gradle 项目,我通常使用该选项gradle run
来运行我的 Java 应用程序。
I have an error in my Java code and I want to debug it, but when I execute gradle run
, the debugger doesn't stop in the breakpoints. In the "Debug as" menu, I don't have anything like gradle debug
.
我的 Java 代码中有错误,我想调试它,但是当我执行时gradle run
,调试器不会在断点处停止。在“调试为”菜单中,我没有像gradle debug
.
How can I debug my application?
如何调试我的应用程序?
回答by Ayon Nahiyan
To debug a Gradle project in Eclipse follow these steps:
要在 Eclipse 中调试 Gradle 项目,请执行以下步骤:
Step 1:
Put these in the build.gradle
file:
第 1 步:
将这些放在build.gradle
文件中:
tasks.withType(JavaExec) {
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9099'
}
}
Step 2:
第 2 步:
From the root of your project run:
从项目的根运行:
gradle -DDEBUG=true run
You will now see something like this in the console:
您现在将在控制台中看到类似的内容:
Listening for transport dt_socket at address: 9099
Step 3:
第 3 步:
Now set breakpoints in Eclipse in your source files.
现在在源文件中的 Eclipse 中设置断点。
Step 4:
第 4 步:
As last step, now right-click in Eclipse on Project > Debug as > Debug configuration > Remote Java application.
作为最后一步,现在在 Eclipse 中右键单击 Project > Debug as > Debug configuration > Remote Java application。
There you have to set these fields:
在那里你必须设置这些字段:
1. Project (this should be set to name of your Eclipse project)
2. Host (here it's localhost)
3. Port (in this example it will be 9099)
Click "Debug". Now your program will stop whenever it hits a breakpoint in Eclipse.
单击“调试”。现在您的程序将在 Eclipse 中遇到断点时停止。
To see details of these steps with an example project see this gradle example on GitHub:
要使用示例项目查看这些步骤的详细信息,请参阅GitHub 上的此 gradle 示例:
回答by Markus Weninger
Even though the accepted answer should work, you can achieve it in a much easier way.
即使接受的答案应该有效,您也可以以更简单的方式实现它。
Just run
gradle run --debug-jvm
. This starts the application in remote debug mode, and you can attach with any remote debugger, e.g., Eclipse, on port5005
.Assuming you use Eclipse as IDE: In Eclipse, go on your Project -> Debug as... -> Debug Configuration -> Remote Java Application. As host set
localhost
, as port5005
, and you are free to go.
就跑
gradle run --debug-jvm
。这将在远程调试模式下启动应用程序,您可以在端口上连接任何远程调试器,例如 Eclipse5005
。假设您使用 Eclipse 作为 IDE:在 Eclipse 中,继续您的项目 -> Debug as... -> Debug Configuration -> Remote Java Application。作为主机集
localhost
,作为端口5005
,你可以自由去。
For more information see the official Gradle Java plugin docregarding testing.
有关更多信息,请参阅有关测试的官方 Gradle Java 插件文档。
[...] can also be enabled at invocation time via the --debug-jvm task option (since Gradle 1.12).
[...] 也可以在调用时通过 --debug-jvm 任务选项启用(自 Gradle 1.12 起)。
回答by Jad Gh
I wasn't able to run the gradle task in debug mode through adding the following code in build.gradle file.
通过在 build.gradle 文件中添加以下代码,我无法在调试模式下运行 gradle 任务。
tasks.withType(JavaExec) {
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}
}
Instead I did the following steps and managed to run it automatically in debug mode :
相反,我执行了以下步骤并设法在调试模式下自动运行它:
- All you need to to do is to go to $GRADLE_HOME/bin.
- Edit the file gradle or gradle.bat depending on your OS.
- Set the JVM parameter in the OS-specific way. For example:
- 您需要做的就是转到 $GRADLE_HOME/bin。
- 根据您的操作系统编辑文件 gradle 或 gradle.bat。
- 以特定于操作系统的方式设置 JVM 参数。例如:
gradle(UNIX) case :
gradle(UNIX)案例:
DEFAULT_JVM_OPTS="-Xdebug-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
gradle.bat(NT) case :
gradle.bat(NT) 案例:
set DEFAULT_JVM_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
. 4. Run the Gradle command from the console.
. 4. 从控制台运行 Gradle 命令。