简体   繁体   English

远程调试 Java 应用程序

[英]Remote debugging a Java application

I have a java application running on linux machine.我有一个在 linux 机器上运行的 java 应用程序。 I run the java application using the following:我使用以下命令运行 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.我已经在这台 Linux 机器上为 TCP 打开了端口 4000。 I use eclipse from Windows XP machine and try to connect to this application.我使用 Windows XP 机器上的 eclipse 并尝试连接到此应用程序。 I have opened the port in windows also.我也在 Windows 中打开了端口。

Both machines are on the LAN but I can't seem to connect the debugger to the Java application.两台机器都在局域网上,但我似乎无法将调试器连接到 Java 应用程序。 What am I doing wrong?我究竟做错了什么?

Edit: I noticed that some people are cutting and pasting the invocation here.编辑:我注意到有些人在这里剪切和粘贴调用。 The answer I originally gave was relevant for the OP only.我最初给出的答案仅与 OP 相关。 Here's a more modern invocation style (including using the more conventional port of 8000):这是一个更现代的调用风格(包括使用更传统的 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:这里有两点:

  1. No spaces in the runjdwp option. runjdwp选项中没有空格。
  2. Options come before the class name.选项位于类名之前。 Any arguments you have after the class name are arguments to your program!在类名之后的任何参数都是程序的参数!

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来自java 技术说明

From 5.0 onwards the -agentlib:jdwp option is used to load and specify options to the JDWP agent.从 5.0 开始,-agentlib:jdwp 选项用于加载和指定 JDWP 代理的选项。 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 之前的版本,使用 -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 是在 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 6 起不再提供的 Java 虚拟机分析器接口 (JVMPI) 和 Java 虚拟机调试接口 (JVMDI)。

Steps:脚步:

  1. Start your remote java application with debugging options as said in above post.使用上面帖子中所述的调试选项启动您的远程 Java 应用程序。
  2. Configure Eclipse for remote debugging by specifying host and port.通过指定主机和端口来配置 Eclipse 以进行远程调试。
  3. Start remote debugging in Eclipse and wait for connection to succeed.在 Eclipse 中启动远程调试并等待连接成功。
  4. Setup breakpoint and debug.设置断点和调试。
  5. If you want to debug from start of application use suspend=y , this will keep remote application suspended until you connect from eclipse.如果您想从应用程序开始调试使用 suspend=y ,这将使远程应用程序暂停,直到您从 eclipse 连接。

See Step by Step guide on Java remote debugging for full details.有关完整详细信息,请参阅有关 Java 远程调试的分步指南

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,端口号足以连接。

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 , 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命令没有 It will pass everything after app.jar as command-line arguments.它会将app.jar之后的所有内容作为命令行参数传递。

This is how you should setup Eclipse Debugger for remote debugging:这是您应该如何设置 Eclipse Debugger 以进行远程调试:

Eclipse Settings:日食设置:

1.Click the Run Button 1.点击运行按钮
2.Select the Debug Configurations 2.选择调试配置
3.Select the “Remote Java Application” 3.选择“远程Java应用程序”
4.New Configuration 4.新配置

  • Name : GatewayPortalProject名称:网关门户项目
  • Project : GatewayPortal-portlet项目:GatewayPortal-portlet
  • Connection Type: Socket Attach连接类型:插座连接
  • Connection Properties: i) localhost ii) 8787连接属性: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.bat file :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 breakpoints in the Java classes you desire to debug.然后,您需要在要调试的 Java 类中设置断点

From Java 9, the JDWP socket connector accepts only local connections by default. 从Java 9开始,JDWP套接字连接器默认情况下仅接受本地连接。 See: http://www.oracle.com/technetwork/java/javase/9-notes-3745703.html#JDK-8041435 参见: http : //www.oracle.com/technetwork/java/javase/9​​-notes-3745703.html#JDK-8041435

So, to enable debug connections from outside, specify *: as address: 因此,要从外部启用调试连接,请指定*:作为地址:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

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 -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.还不够,因为它现在(至少在 unix/osx 机器上)绑定到 localhost,所以你只能从 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:从我认为 Java 9 开始,您需要执行以下操作:

-agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n -agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n

or give an ip that it needs to bind on for hat *或者给出一个需要绑定到帽子上的ip *

The best option of debugging is to create logs and analyze logs. 调试的最佳选择是创建日志并分析日志。 But in some cases we need to debug web application deployed on server. 但是在某些情况下,我们需要调试服务器上部署的Web应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM