简体   繁体   English

如何在不使用IDE的情况下调试Java程序?

[英]How to debug a Java program without using an IDE?

How do you turn the debug on and off within your Java program ? 如何在Java程序中打开和关闭调试? How do you turn the debug on and off without recomipiling the java program? 如何在不重新编写java程序的情况下打开和关闭调试?

A setting to the Java virtual machine allows debuggers eg jdb to attach. Java虚拟机的设置允许调试器,例如jdb附加。 See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html 请参阅http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html

This is the important bit: 这是重要的一点:

Running MyClass in a JVM with debugging enabled: 在启用调试的情况下在JVM中运行MyClass:

java -agentlib:jdwp=transport=dt_shmem,address=jdbconn,server=y,suspend=n MyClass

Using the jdb debugger 使用jdb调试器

jdb -attach jdbconn 

Note: These option settings are for a connection JVM <-> debugger on the local machine via shared memory, other useful settings allow to connect to a JVM on a remote machine via network sockets. 注意:这些选项设置用于通过共享内存在本地计算机上连接JVM < - >调试程序,其他有用的设置允许通过网络套接字连接到远程计算机上的JVM。

There are two things you have to consider: 你必须考虑两件事:

  • you need only to compile your code once in order to have debug information; 您只需要编译一次代码以获得调试信息; and by default, source file and line number debug information are generated ( documentation ); 默认情况下,生成源文件和行号调试信息( 文档 );
  • the ability to debug or not is controlled when you invoke the JVM . 调用或不调试的能力在您调用JVM时受到控制。

For Oracle's JVM, this set of options will allow to plug in a debugger implementing JDWP (Java Debug Wire Protocol) on port 12345 (TCP): 对于Oracle的JVM,这组选项将允许插入在端口12345(TCP)上实现JDWP(Java调试线协议)的调试器:

-Xdebug -Xrunjdwp:server=y,suspend=n,transport=dt_socket,port=12345

Note the suspend=n ; 注意suspend=n ; if you do suspend=y , the JVM will not run unless you actually connect a debugger... 如果你suspend=y ,除非你实际连接调试器,否则JVM不会运行...

Finally, a good link explaining the nooks and crannies behind JDWP, JVM[DPT]I: here 最后,一个很好的链接解释了JDWP背后的角落和缝隙,JVM [DPT] I: 这里

Here is also a tutorial for jdb, already mentioned by other answers. 这里也是jdb的教程,其他答案已经提到了。

Use jdb to debug from the command line. 使用jdb从命令行进行调试。

That being said, I have no idea what "turn the debug off and on" means. 话虽如此,我不知道“关闭和打开调试”意味着什么。

Without using IDE to debug 不使用IDE进行调试

1)you can write your java program with Assertions. 1)你可以用Assertions编写java程序。 When ever you want you can enable / disable them. 什么时候你想要你可以启用/禁用它们。

2) you can use logs configured with log4j.properties. 2)您可以使用配置了log4j.properties的日志。 In your java program you can specify info and debug logs whenever you want you can simple configure in log4j.properties when you want to display debug or info logs etc... 在java程序中,您可以随时指定信息和调试日志,当您想要显示调试或信息日志等时,可以在log4j.properties中进行简单配置...

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

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