远程调试 Java 应用程序
我有一个在linux机器上运行的java应用程序。 我使用以下命令运行 java 应用程序:
java myapp -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000, suspend=n
我已在此 Linux 计算机上为 TCP 打开端口 4000。 我在 Windows XP 机器上使用 eclipse 并尝试连接到该应用程序。 我也在windows中打开了端口。
两台机器都在 LAN 上,但我似乎无法将调试器连接到 Java 应用程序。 我究竟做错了什么?
I have a java application running on linux machine. I run the java application using the following:
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.
Both machines are on the LAN but I can't seem to connect the debugger to the Java application. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于遇到问题的每个人,如果您确实从一台机器到另一台机器进行远程调试,那么使用:
-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
是不够的,因为现在绑定了(至少在unix/osx机器上)到本地主机,所以你只能从本地主机连接到它。
如果您尝试远程调试,那么您将收到连接被拒绝的信息。 从我认为Java 9开始你需要做:
-agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n
或给出一个它需要绑定的IP *
for everybody that has the problem that if you really do remote debugging from 1 machine to the other then using :
-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
is not enough because that binds now (at least on unix/osx machines) to localhost so you can only connect to it from localhost.
If you try to remote debug this then you will get connection refused for this. From i think Java 9 on you need to do:
-agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n
or give an ip that it needs to bind on for hat *
这里的每个解决方案都描述了应用程序启动后附加调试器的方法。
在“调试器监听”设置中,您需要设置 server=n 和 suspend=n。 如果不是,Gradle 将抛出权限被拒绝错误。
构建.gradle:
Every solution here is describing the method to attach debugger after the application starts.
In the "debugger listening" setup you need to set server=n and suspend=n. If not the Gradle will throw permission denied error.
build.gradle:
编辑:我注意到有些人在这里剪切并粘贴了调用。 我最初给出的答案仅与OP相关。 这是一种更现代的调用风格(包括使用更传统的端口 8000):
注意: 使用
address=8000
(如上面使用的),调试服务器将仅侦听localhost
(请参阅设置哪些 Java 命令行选项以允许远程调试 JVM?) 。 如果您希望服务器侦听所有接口,以便能够通过网络进行调试,请使用address=*:8000
。 显然只能在受限制的、可信的网络上执行此操作...原始答案如下。
试试这个:
这里有两点:
runjdwp
选项中没有空格。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):
Note: With
address=8000
(as used above), the debug server will only listen onlocalhost
(see What are Java command line options to set to allow JVM to be remotely debugged?). If you want the server to listen on all interfaces, to be able to debug across the network, useaddress=*:8000
. Obviously only do this on a restricted, trusted network...Original answer follows.
Try this:
Two points here:
runjdwp
option.对于 JDK 1.3 或更早版本:
对于 JDK 1.4
对于较新的 JDK:
请根据您的需要更改端口号。
来自
还有一件事需要注意,来自 JVM Tool 接口文档:
For JDK 1.3 or earlier :
For JDK 1.4
For newer JDK :
Please change the port number based on your needs.
From java technotes
One more thing to note, from JVM Tool interface documentation:
涵盖 Java >= 9 的答案:
对于 Java 9+,JVM 选项需要稍作更改,方法是将托管 JVM 的计算机的 IP 地址作为地址前缀,或者只是
*:
这是由于 中记录的更改所致https://www.oracle.com/java/technologies/javase/9-all-relnotes.html#JDK-8041435。
对于Java< 9、端口号足够连接。
Answer covering 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
*
:This is due to a change noted in https://www.oracle.com/java/technologies/javase/9-all-relnotes.html#JDK-8041435.
For Java < 9, the port number is enough to connect.
步骤:
请参阅Java 远程调试分步指南 了解完整详细信息。
Steps:
See Step by Step guide on Java remote debugging for full details.
我想强调参数的顺序很重要。
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar app.jar
命令打开调试器端口,但是
java -jar app.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
命令不执行。 它将把 app.jar 之后的所有内容作为命令行参数传递。I'd like to emphasize that order of arguments is important.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar app.jar
command opens debugger port,but
java -jar app.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
command doesn't. It will pass everything after app.jar as command-line arguments.这是您应该如何设置 Eclipse 调试器以进行远程调试:
Eclipse 设置:
1.单击“运行”按钮
2.选择调试配置
3.选择“远程Java应用程序”
4.新配置
i) localhost ii) 8787
对于 JBoss:
1.更改
/path/toJboss/jboss-eap-6.1/bin/standalone.conf
在你的虚拟机中如下:通过删除 # 取消注释以下行:
For Tomcat :
在 catalina.bat 文件中:
步骤 1:
步骤 2:
步骤 3: 从命令提示符运行 Tomcat,如下所示:
然后您需要在要调试的 Java 类中设置断点。
This is how you should setup Eclipse Debugger for remote debugging:
Eclipse Settings:
1.Click the Run Button
2.Select the Debug Configurations
3.Select the “Remote Java Application”
4.New Configuration
i) localhost ii) 8787
For 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 #:
For Tomcat :
In catalina.bat file :
Step 1:
Step 2:
Step 3: Run Tomcat from command prompt like below:
Then you need to set breakpoints in the Java classes you desire to debug.