`

Java debugging tutorial - 10 tips on debugging in java with example

阅读更多

Java debugging tutorial - 10 tips on debugging in java with example

How to debug java program in Eclipse

Debugging is a must have skill for any java developer. Having ability to debug java program enables to find you any subtle bug which is not visible during code review or comes when a particular condition offer, This becomes even more important if you are working in high frequency trading or electronic trading system project where time to fix a bug is very less and bug usually comes on production environment and doesn't appear in your Windows XP machine. in my experience debugging java application also helps you understand flow of java program. In this java tutorial we will see how to debug a java program, setting up remote debugging in java and some java debugging tips on Eclipse and Netbeans IDE. It’s also good to know various java debug tool available and how java debugger or jdb works but it’s not mandatory for doing debugging in Java. To start java debugging you just needs your project to be configured in a modern IDE like eclipse and Netbeans and you are ready to debug java program.


Java debugging tools

Java debugging tutorial example, java debugger, java debugging tools and tipsI mostly used Eclipse IDE and Netbeans IDE for java development and these IDE have great support for java debugging. They allow you to set various breakpoints like line breakpoint, conditional breakpoint or exception breakpoint. I prefer Eclipse over netbeans because of its seamless integration with remote debugging because most of the time your application will run on Linux machine and you might not have local version running on your machine, in such scenario remote debugging is extremely useful. You can check how to setup java remote debugging in eclipse for step by step guide on setting remote debugging in eclipses. Apart from Eclipse and Netbeans IDE you can also use Java debugger jdb which is a simple command line based java debugger and based on java platform debugging architecture and can be used to debug java program locally or remotely.

Java debug options

If you are not using any IDE for java debugging locally you need to provide java debug option while starting your program. You need to provide java debug option also if you are setting up remote debugging session or using jdb for java debugging. Following are the two java debugging option which needs to be provided to java program:
Debug Options Purpose
Xdebug         Used to run java program in debug mode
Xrunjdwp:transport=dt_socket,server=y,suspend=n        Loads in Process debugging libraries and specifies the kind of connection to be made.
Suspend=y and n is quite useful for debugging from start or debugging at any point.

Using jdb to debug java application

1) Start your java program with two options provided above for example, below command will start StockTrading java program in debug mode.

 % java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n StockTrading

After starting your java application in debug mode you can attach java debugger "jdb" to the VM with the following command:

 % jdb -attach 8000

You can check the jdb manual page for complete detail on how to do java debugging with jdb.

Java remote debugging with eclipse

This is another cool feature of eclipse which allows you to connect your java application running on remote host and do remote debugging. You just need to start your java application with the java debug option discussed above and then connect your application from eclipse into specified port. You can check below link for step by step guide on java remote debugging with eclipse.


Debugging java application in Eclipse and Netbeans

Debugging java application locally on any IDE like Eclipse or Netbeans it’s very simple, just select the project and click debug or use debug shortcut provided by IDE. You can also debug a single java class with main method. In Eclipse just right click and select "Debug as Java Application".

10 practical Java debugging tips

Now let's see some java debugging tips which I used while doing debugging in Java in eclipse.

1) Use conditional breakpoint
Eclipse allows you to setup conditional break point for debugging java program, which is a breakpoint with condition and your thread will only stop at specified line if condition matches instead of just stopping on that line like in case of line breakpoint. To setup a conditional breakpoint just double click on any line where you want to setup a breakpoint and then right click --> properties and then insert the condition. Now program will only stop when that particular condition is true and program is running on debug mode.





2) Use Exception breakpoint
How many times you have frustrated with a NullPointerException and you don't know the source from where the exception is coming. Exception breakpoints are just made for such situation. Both Eclipse and Netbeans allows you to setup Exception breakpoint. You can setup Exception breakpoint based on java exception like NullPointerException or ArrayIndexOutOfBoundException. You can setup Exception breakpoint from breakpoint window and your program will stop when you start it on debug mode and exception occurs.
 


 
3) Step over, Step Into
These are simply great debugging options available in any Java IDE, extremely useful if you are debugging multi-threaded application and want to navigate step by step.

4) Stopping for a particular Thread
This is my own custom made java debugging tips which I made using conditional breakpoints. since most of my projects are multi-threaded java programs and I want only a particular thread to stop on a particular line, for doing that I setup a conditional breakpoint on that line and put Thread.currentThread().getName().equals("TestingThread") and it works fantastically.

5) Inspect and Watch
These are two menu options which I use to see the value of expression during debugging java program. I just select the statement, right click and inspect and it will show you the value of that statement at debugging time. You can also put watch on that and that condition and its value will appear on watch window.

6) Suspending and resuming thread
You can suspend and resume any thread while debugging java program from debug window. Just right click on any thread and select either suspends or resume. This is also very useful while debugging multi-threading program and simulating race conditions.

7) Using logical structure
Logical structure option is very useful for examining contents inside java collection classes like java hasmap or Java Arraylist during java debugging. Logical view will show the contents like key and value of hashmap instead of showing full details of hashmap which we may not be interested, you can enable and disable logical view from variables window.

8) Step filtering
When we do Step Into on process debugging java program control goes form one class to other and it eventually go to JDK classes like System or String. Some time we just to remain in our application and don't want to navigate into JDK System classes in that case Step filtering is great you can just filter out JDK class from Step into. You can setup step filtering from preferences àJavaàDebugàStep Filtering and enable and disable it from Debug window.

9) Copy Stack
While debugging java program if you want to copy the stack of a thread which hit the breakpoint and suspended you do so by "Copy Stack" option. Just right click on Thread on Debug Window and select "Copy Stack".

10) Last tip is use java debugging as last option and not the first option because it’s very time consuming, especially remote java debugging which takes a lot of time if network latency is very high between local and remote host. Try to identify problem by looking at code it would be very handy and quick.

lastly java debugging is real fun so definitely try it few times to get hold of it and please share some other java debugging tips you use on your daily life.


Read more: http://javarevisited.blogspot.com/2011/07/java-debugging-tutorial-example-tips.html#ixzz1sqrIMpHS
  • 大小: 8.6 KB
  • 大小: 82.8 KB
  • 大小: 9.7 KB
分享到:
评论

相关推荐

    Unity-debugging-5.x.zip

    "Unity-debugging-5.x.zip"这个压缩包可能包含了一组针对Unity 5.x版本的调试工具和资源,特别是针对"dnspy"的mono.dll文件。 dnspy是一款开源的.NET反编译器和调试器,它允许开发者查看、编辑、调试和分析.NET程序...

    Debugging-with-GDB.zip_Debugging with GDB

    《使用GDB进行调试》 GDB(GNU Debugger)是开源社区GNU开发的一款强大的源代码级调试器,广泛应用于C、C++等编程语言。它允许程序员在运行时检查和控制程序,查找并修复错误,从而提高软件的质量和可靠性。...

    ug936-vivado-tutorial-programming-debugging.pdf

    该教程“UG936-vivado-tutorial-programming-debugging.pdf”聚焦于Vivado中的编程与调试流程,版本号为2022.2,发布日期为2022年11月16日。Xilinx致力于创建一个包容性的工作环境,因此在产品和相关材料中移除可能...

    Unity-debugging-2019.x.zip

    "Unity-debugging-2019.x.zip"这个压缩包很可能包含了一些关于在Unity 2019版本中进行调试的资源和教程,特别是使用dnSpy工具来调试Unity引擎中的"mono.dll"。 dnSpy是一款强大的.NET反编译器和调试器,特别适合于...

    Fundamentals_in_debugging_nRF5x_systems - Hard Fault on nRF52.pdf

    “Fundamentals_in_debugging_nRF5x_systems - Hard Fault on nRF52.pdf”这一标题明确指出了文档的主要内容是关于nRF5x系统(特别是nRF52系列)的基本调试技术,并着重于解释硬故障(Hard Fault)的问题。...

    Unity-debugging-2017.x.zip

    "Unity-debugging-2017.x.zip" 这个压缩包显然是针对Unity 2017版本的一个调试工具集合,特别提到了"dnspy"和"mono.dll",这两个在Unity开发中扮演着关键角色。 dnSpy是一款开源的.NET反编译器和调试器,特别适合...

    Unity-debugging-dlls

    Unity-debugging-4.x Unity-debugging-5.x Unity-debugging-2017.x Unity-debugging-2018.x Unity-debugging-2019.x

    Debugging-with-GDB-带书签

    "Targets"和"Remote Debugging"分别说明了指定调试目标和远程调试的技巧,这在跨平台或网络环境下的开发中十分关键。最后,"Configurations"可能涉及GDB的配置选项和自定义,以适应不同用户的需求和工作流。 总之,...

    Core Java Volume II 最新第8版 part1全两卷 (附随书源码)

    Fully updated for the new Java SE 6 platform, this no-nonsense tutorial and reliable reference illuminates the most important language and library features with thoroughly tested real-world examples....

    Debugging With GDB中文版

    This is the Tenth Edition, of Debugging with GDB: the GNU Source-Level Debugger for GDB (GDB) Version 8.1.90.20180814-git. Copyright © 1988-2018 Free Software Foundation, Inc. Permission is granted...

    Computer Graphics Programming in OpenGL with Java

    This new edition provides step-by-step instruction on modern 3D graphics shader programming in OpenGL, along with its theoretical foundations. It is appropriate both for computer science undergraduate...

    Debugging with GDB --2007年

    Debugging programs with multiple processes . . . . . . . . . . . . 23 23 24 25 26 26 27 28 28 31 ii 5 Debugging with GDB Stopping and Continuing . . . . . . . . . . . . . . . . . . 33 5.1 Breakpoints....

    tensorflow-tutorial-samples

    在本教程中,我们将深入探讨"tensorflow-tutorial-samples",这是一个专为TensorFlow 2.0设计的入门教程和实战案例集。TensorFlow是由谷歌大脑团队开发的一个强大的开源库,用于数值计算和大规模机器学习任务。这个...

    debugging-practice-debugging-with-pry-lab-online-web-prework

    调试如果克隆此实验室并运行测试,您会注意到一些事情:一切都坏了。 像,坏透了。 你猜怎么着? 修复它就在您身上! 请记住,该实验室不仅要查找和修复错误,还应了解如何查找这些错误是什么。...

    Windows Debugging Tool-x86&&x86_64

    Windows Debugging tool 32位和64位的都有,包括适用于windows7的和支持windows8的两种版本,安装好任意一种后,即可在QT Creator中使用CDB调试,当然也可以4种都安装!

    Unity-debugging-2018.x.zip

    这个"Unity-debugging-2018.x.zip"压缩包显然与Unity的调试功能有关,特别是针对2018版本。Unity的调试工具对于优化代码、查找错误和提高性能至关重要。 首先,我们要关注的是“dnSpy”这个工具。dnSpy是一款开源的...

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Debugging Java EE Applications 45 Part II: The Web Tier 47 Chapter 3: Getting Started with Web Applications 49 Web Applications 50 Web Application Lifecycle 51 Web Modules: The hello1 Example ...

Global site tag (gtag.js) - Google Analytics