JAVA 远程Debug

开启JVM的远程Debug模式, 在启动JVM的时候加上参数:

// 非阻塞方式,这样启动jvm后,可以再任意时间attach到server上
-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=11312,server=y,suspend=n
// 阻塞debug,启动JVM后,需要远程连接attach到相应的端口,JVM才会继续执行。
-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=11312,server=y,suspend=y

第二条特别适合调试类初始化的程序模块。

在eclipse中,bug项里有Debug Configurations… 选中。然后选中remove java application, 新建一个

Project就是你要远程调试的 JAVA源码文件工程。  HOST就是那个server所在的机器名或者IP。 PORT就是
上面的address项11312。Connection Type就写Standard. 然后点击debug就可以。

如果debug后,有连接错误。 1. 要查看server或者service是否已经启动。 2. 要看是否配置正确。3.检测端口是否被其他程序占用了。

如果上面的检测完毕,还是连接失败。那么重启eclipse 或者 重启server. 这两个方法就可以解决问题。

另外

-Xnoagent
这一个选项在远程debug中一般不需要。其含义为Disables VM support for oldjdb。禁用旧的java debug。

下面是各个options的详解。摘自
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jdb.html

-Xdebug

Enables debugging support in the VM

-Xrunjdwp:transport=dt_shmem,server=y,suspend=n

Loads in-process debugging libraries and specifies the kind of connection to
be made.

-Xnoagent

Disables VM support for ** oldjdb **

-Djava.compiler=NONE

Disables the JIT compiler. This is required for debugging under the classic VM