Print more logging messages during debugging

Sometimes, in debug mode,  more logging messages could be very helpful. We can change code to add more logs. But it’s not efficient as the code need to be re-compiled and later some log message may need to be removed after debugging.

Java debugger provides very powerful way to achieve this, which allows to print more messages and even more.

Take Eclipse for example, here I have a simple HelloWorld to calculate the sum from 0 to 99. In debugging, I want to see how sum changed in the loop but I don’t want to stop application and click “Continue” every time.

12-12-2014 5-31-25 PM

So first, I added a break point on line 8. Next, I edited the “Breakpoint Properties”. In the conditional text input, you can print some message first and make the return of expression as false.

12-12-2014 5-35-40 PM

To return false will make debugger not stop at line 8, but just print the log messages I want.

12-12-2014 5-37-57 PM

You can see it’s totally non-intrusive. Actually, you can put more complicated statements there and also you can control when to return true or false. It’s very flexible. In other IDEs, such as JetBrains IDEA and Netbeans, there are similar functions.

JPDA调试Eclipse RCP程序

当你已经有了一个打包好的Eclipse RCP程序,如何利用JPDA调试呢?其实方法和《使用JPDA进行Java程序的Debug》中说的基本一样。有几点不同。


RCP程序在打包完成之后,会同时生成.ini文件。JPDA的参数是不能放在这个文件里的,因为这是为OSGi Runtime准备的参数。我们需要在Java命令行里做,所以要手动启动RCP程序,不能用打包好的exe文件。

其实也就是手动把Equinox Launcher运行起来,然后Eclipse IDE就可以和RCP程序连接了。


在设置Remote Java Application时,可以直接加入Source。