远程调试 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/975271/
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
Remote debugging a Java application
提问by
I have a java application running on linux machine. I run the java application using the following:
我有一个在 linux 机器上运行的 java 应用程序。我使用以下命令运行 java 应用程序:
java myapp -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000, suspend=n
I have opened port 4000 for TCP on this Linux machine. I use eclipse from Windows XP machine and try to connect to this application. I have opened the port in windows also.
我已经在这台 Linux 机器上为 TCP 打开了端口 4000。我使用 Windows XP 机器上的 eclipse 并尝试连接到此应用程序。我也在 Windows 中打开了端口。
Both machines are on the LAN but I can't seem to connect the debugger to the Java application. What am I doing wrong?
两台机器都在局域网上,但我似乎无法将调试器连接到 Java 应用程序。我究竟做错了什么?
采纳答案by Chris Jester-Young
Edit:I noticed that some people are cutting and pasting the invocation here. The answer I originally gave was relevant for the OP only. Here's a more modern invocation style (including using the more conventional port of 8000):
编辑:我注意到有些人在这里剪切和粘贴调用。我最初给出的答案仅与 OP 相关。这是一个更现代的调用风格(包括使用更传统的 8000 端口):
java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n <other arguments>
Original answer follows.
原答案如下。
Try this:
尝试这个:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=n myapp
Two points here:
这里有两点:
- No spaces in the
runjdwp
option. - Options come before the class name. Any arguments you have after the class name are arguments to your program!
runjdwp
选项中没有空格。- 选项位于类名之前。在类名之后的任何参数都是程序的参数!
回答by Gaurav
Steps:
脚步:
- Start your remote java application with debugging options as said in above post.
- Configure Eclipse for remote debugging by specifying host and port.
- Start remote debugging in Eclipse and wait for connection to succeed.
- Setup breakpoint and debug.
- If you want to debug from start of application use suspend=y , this will keep remote application suspended until you connect from eclipse.
- 使用上面帖子中所述的调试选项启动您的远程 Java 应用程序。
- 通过指定主机和端口来配置 Eclipse 以进行远程调试。
- 在 Eclipse 中启动远程调试并等待连接成功。
- 设置断点和调试。
- 如果您想从应用程序开始调试使用 suspend=y ,这将使远程应用程序暂停,直到您从 eclipse 连接。
See Step by Step guide on Java remote debuggingfor full details.
有关完整详细信息,请参阅有关 Java 远程调试的分步指南。
回答by Sairam Krish
For JDK 1.3 or earlier :
对于 JDK 1.3 或更早版本:
-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006
For JDK 1.4
对于 JDK 1.4
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006
For newer JDK :
对于较新的 JDK :
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6006
Please change the port number based on your needs.
请根据您的需要更改端口号。
From java technotes
From 5.0 onwards the -agentlib:jdwp option is used to load and specify options to the JDWP agent. For releases prior to 5.0, the -Xdebug and -Xrunjdwp options are used (the 5.0 implementation also supports the -Xdebug and -Xrunjdwp options but the newer -agentlib:jdwp option is preferable as the JDWP agent in 5.0 uses the JVM TI interface to the VM rather than the older JVMDI interface)
从 5.0 开始,-agentlib:jdwp 选项用于加载和指定 JDWP 代理的选项。对于 5.0 之前的版本,使用 -Xdebug 和 -Xrunjdwp 选项(5.0 实现还支持 -Xdebug 和 -Xrunjdwp 选项,但较新的 -agentlib:jdwp 选项更可取,因为 5.0 中的 JDWP 代理使用 JVM TI 接口VM 而不是旧的 JVMDI 接口)
One more thing to note, from JVM Tool interface documentation:
还有一件事要注意,来自JVM 工具接口文档:
JVM TI was introduced at JDK 5.0. JVM TI replaces the Java Virtual Machine Profiler Interface (JVMPI) and the Java Virtual Machine Debug Interface (JVMDI) which, as of JDK 6, are no longer provided.
JVM TI 是在 JDK 5.0 中引入的。JVM TI 取代了自 JDK 6 起不再提供的 Java 虚拟机分析器接口 (JVMPI) 和 Java 虚拟机调试接口 (JVMDI)。
回答by Pritam Banerjee
This is how you should setup Eclipse Debugger for remote debugging:
这是您应该如何设置 Eclipse Debugger 以进行远程调试:
Eclipse Settings:
日食设置:
1.Click the Run Button
2.Select the Debug Configurations
3.Select the “Remote Java Application”
4.New Configuration
1.单击运行按钮
2.选择调试配置
3.选择“远程 Java 应用程序”
4.新建配置
- Name : GatewayPortalProject
- Project : GatewayPortal-portlet
- Connection Type: Socket Attach
- Connection Properties: i) localhost ii) 8787
- 名称:网关门户项目
- 项目:GatewayPortal-portlet
- 连接类型:插座连接
- 连接属性:i) 本地主机 ii) 8787
For JBoss:
对于 JBoss:
1.Change the /path/toJboss/jboss-eap-6.1/bin/standalone.conf
in your vm as follows:
Uncomment the following line by removing the #:
1./path/toJboss/jboss-eap-6.1/bin/standalone.conf
按如下方式更改虚拟机中的 :通过删除 # 取消注释以下行:
JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"
For Tomcat :
对于 Tomcat :
In catalina.batfile :
在catalina.bat文件中:
Step 1:
第1步:
CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
Step 2:
第2步:
JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
Step 3: Run Tomcat from command prompt like below:
第 3 步:从命令提示符运行 Tomcat,如下所示:
catalina.sh jpda start
Then you need to set breakpointsin the Java classes you desire to debug.
然后,您需要在要调试的 Java 类中设置断点。
回答by M Anouti
Answer covering Java >= 9:
答案涵盖 Java >= 9:
For Java 9+, the JVM option needs a slight change by prefixing the address with the IP address of the machine hosting the JVM, or just *
:
对于 Java 9+,JVM 选项需要通过使用托管 JVM 的机器的 IP 地址作为地址前缀,或者只是*
:
-agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n
This is due to a change noted in https://www.oracle.com/technetwork/java/javase/9-notes-3745703.html#JDK-8041435.
这是由于https://www.oracle.com/technetwork/java/javase/9-notes-3745703.html#JDK-8041435 中指出的更改。
For Java < 9, the port number is enough to connect.
对于 Java < 9,端口号足以连接。
回答by MrBlack
I'd like to emphasize that order of arguments is important.
我想强调的是,论证的顺序很重要。
For me java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar app.jar
command opens debugger port,
对我来说, java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar app.jar
命令打开调试器端口,
but java -jar app.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
command doesn't.
但java -jar app.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
命令没有。