远程调试 Java 应用程序

发布于 2024-07-23 12:27:42 字数 326 浏览 9 评论 0原文

我有一个在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

べ映画 2024-07-30 12:27:43

对于遇到问题的每个人,如果您确实从一台机器到另一台机器进行远程调试,那么使用:

-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 *

ま昔日黯然 2024-07-30 12:27:43

这里的每个解决方案都描述了应用程序启动后附加调试器的方法。
在“调试器监听”设置中,您需要设置 server=n 和 suspend=n。 如果不是,Gradle 将抛出权限被拒绝错误。

构建.gradle:

run {
    jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=n,suspend=n,address=5005"
}

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:

run {
    jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=n,suspend=n,address=5005"
}
ゞ记忆︶ㄣ 2024-07-30 12:27:42

编辑:我注意到有些人在这里剪切并粘贴了调用。 我最初给出的答案仅与OP相关。 这是一种更现代的调用风格(包括使用更传统的端口 8000):

java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
<other arguments>

注意: 使用 address=8000 (如上面使用的),调试服务器将仅侦听localhost(请参阅设置哪些 Java 命令行选项以允许远程调试 JVM?) 。 如果您希望服务器侦听所有接口,以便能够通过网络进行调试,请使用 address=*:8000。 显然只能在受限制的、可信的网络上执行此操作...

原始答案如下。


试试这个:

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=n myapp

这里有两点:

  1. runjdwp 选项中没有空格。
  2. 选项位于类名称之前。 类名后面的任何参数都是程序的参数!

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):

java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
<other arguments>

Note: With address=8000 (as used above), the debug server will only listen on localhost (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, use address=*:8000. Obviously only do this on a restricted, trusted network...

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.
  2. Options come before the class name. Any arguments you have after the class name are arguments to your program!
后来的我们 2024-07-30 12:27:42

对于 JDK 1.3 或更早版本:

-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006

对于 JDK 1.4

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006

对于较新的 JDK:

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

请根据您的需要更改端口号。

来自

从 5.0 开始,使用 -agentlib:jdwp 选项来加载和指定
JDWP 代理的选项。 对于 5.0 之前的版本,-Xdebug 和
使用 -Xrunjdwp 选项(5.0 实现还支持 -Xdebug 和 -Xrunjdwp 选项,但较新的 -agentlib:jdwp 选项更可取,因为 5.0 中的 JDWP 代理使用 JVM TI 接口
VM 而不是旧的 JVMDI 接口)

还有一件事需要注意,来自 JVM Tool 接口文档

JVM TI 是在 JDK 5.0 中引入的。 JVM TI 取代了 Java 虚拟机分析器接口 (JVMPI) 和 Java 虚拟机调试接口 (JVMDI),从 JDK 6 开始,不再提供这两个接口。

For JDK 1.3 or earlier :

-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006

For JDK 1.4

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006

For newer 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)

One more thing to note, from JVM Tool interface documentation:

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.

清醇 2024-07-30 12:27:42

涵盖 Java >= 9 的答案:

对于 Java 9+,JVM 选项需要稍作更改,方法是将托管 JVM 的计算机的 IP 地址作为地址前缀,或者只是 *:

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

这是由于 中记录的更改所致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 *:

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

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.

森罗 2024-07-30 12:27:42

步骤:

  1. 使用上面的帖子中所述的调试选项启动远程 java 应用程序。
  2. 通过指定主机和端口配置 Eclipse 以进行远程调试。
  3. 在Eclipse中启动远程调试,等待连接成功。
  4. 设置断点并调试。
  5. 如果您想从应用程序启动时使用 suspend=y 进行调试,这将使远程应用程序保持挂起状态,直到您从 eclipse 连接为止。

请参阅Java 远程调试分步指南 了解完整详细信息。

Steps:

  1. Start your remote java application with debugging options as said in above post.
  2. Configure Eclipse for remote debugging by specifying host and port.
  3. Start remote debugging in Eclipse and wait for connection to succeed.
  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.

See Step by Step guide on Java remote debugging for full details.

半夏半凉 2024-07-30 12:27:42

我想强调参数的顺序很重要

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.

李不 2024-07-30 12:27:42

这是您应该如何设置 Eclipse 调试器以进行远程调试:

Eclipse 设置:

1.单击“运行”按钮
2.选择调试配置
3.选择“远程Java应用程序”
4.新配置

  • 名称:GatewayPortalProject
  • 项目:GatewayPortal-portlet
  • 连接类型:Socket Attach
  • 连接属性:
    i) localhost ii) 8787

对于 JBoss:

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 :

catalina.bat 文件中:

步骤 1:

CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

步骤 2:

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

步骤 3: 从命令提示符运行 Tomcat,如下所示:

catalina.sh jpda start

然后您需要在要调试的 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

  • Name : GatewayPortalProject
  • Project : GatewayPortal-portlet
  • Connection Type: Socket Attach
  • Connection Properties:
    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 #:

JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"

For Tomcat :

In catalina.bat file :

Step 1:

CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

Step 2:

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

Step 3: Run Tomcat from command prompt like below:

catalina.sh jpda start

Then you need to set breakpoints in the Java classes you desire to debug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文