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 tips I 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
1705028(1).PDF 立即下载

相关推荐

东汉AD35年世界历史地图矢量文件SHP格式.zip

本资源为《东汉AD35年世界历史地图矢量文件SHP格式》压缩包,文件大小约 29.99 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见JSON 文件、属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

Java Debugging with Eclipse - Tutorial

1. Overview Debugging allows you to run the program interactively and to watch the source code and the variables during this execution. A Java program can be started in "Debug mode". You can s

supercena163的专栏 886

基于FastAPI Vue3 TypeScript的企业级全栈底座:类型共享、权限完整,三端同源 一套代码交付Web/H5/小程序,AI代码生成 RBAC Docker开箱即用一套开源的快速开发平台

基于FastAPI Vue3 TypeScript的企业级全栈底座:类型共享、权限完整,三端同源。一套完全开源的快速开发平台,提供免费使用。它结合了现代、高性能的技术栈,后端采用Fastapi + SQLAlchemy,前端采用基于 vue3 + typescript + vite + pinia + Element-Plus。

从崩溃到掌控:Java Debugging实战指南(2025版)

是否曾面对满屏报错无从下手?花费数小时寻找一个隐藏的逻辑错误?Debugging(调试)作为软件开发的核心技能,却常常被开发者忽视系统学习。根据JetBrains 2024开发者调查,**73%的Java开发者**承认调试占据日常工作30%以上时间,但仅12%接受过正规调试培训。本文将彻底改变你的调试方式,从被动试错转变为主动诊断,让你在1小时内解决过去需要一整天排查的问题。 **读完本文你将掌...

gitblog_00024的博客 307

YOLO26算法室内办公环境二维码目标检测+训练好的模型+199张数据集+pyqt可视化界面.zip

附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[二维码(QRCode)] · 总计:199 张 · 测试集:20 张 · 验证集:40 张 · 训练集:139 张 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 56 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9921** mAP50-95 | 0.8216 Precision | 0.9731 Recall | 1.0000 train/box_loss | 0.6220 train/cls_loss | 0.6579 val/box_loss | 0.7042 val/cls_loss | 0.7743 【训练过程分析】 从整体上看,模型在训练过程中表现稳定,尽管在某些阶段出现了轻微的波折,但总体上遵循了预设的收敛路径。前几轮中,loss 的急剧下降让人感觉仿佛是在玩“过山车”,尤其是在训练的前 10 轮,loss 从初始值 2.0 逐渐降到 1.0 以下,这表明模型在快速学习数据的基本特征。然而,从第 15 轮开始,loss 值并没有进一步明显下降,而是趋于平稳,这表明模型已经初步收敛。 在训练曲线的中期,从第 20 轮到第 35 轮,loss 并没有出现大幅波动或平台期。这个阶段的曲线相对平滑,loss 值基本维持在 0.6 至 0.7 之间,这表明模型的泛化能力在逐渐提升。尽管如此,这个阶段确实出现了一些细微的震荡,特别是在第... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。

东汉AD185年世界历史地图矢量文件SHP格式.zip

本资源为《东汉AD185年世界历史地图矢量文件SHP格式》压缩包,文件大小约 28.48 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

Debugging the Java HotSpot VM

Open Heart Surgery: Analyzing and Debugging the Java HotSpot VM at the OS Level https://www.youtube.com/watch?v=k7IX_diKCEo

weixin_34224941的博客 118

东汉AD155年世界历史地图矢量文件SHP格式.zip

本资源为《东汉AD155年世界历史地图矢量文件SHP格式》压缩包,文件大小约 28.37 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

十六、代码校验(4)

● 调试 ○ 使用 JDB 调试 ○ 图形化调试器

走在bug的路上 6853

bitcoin(C++)

「bitcoin(C++)」是开源项目(C++)。项目简介:Bitcoin Core integration/staging tree源码完整,下载解压即可查看使用,适合学习参考、课程设计与二次开发。

AI第三篇:ScaBench-用真实审计竞赛数据构建可复现的 AI 审计评测集

AI第三篇:ScaBench-用真实审计竞赛数据构建可复现的 AI 审计评测集

汽车专用零部件项目申报材料清单.docx

汽车专用零部件项目申报材料清单

演讲脱敏训练怎么坚持.pdf

演讲脱敏训练怎么坚持

基于粒子群PSO、灰狼GWO、鲸鱼WOA、哈里斯鹰HHO、蜣螂DBO、麻雀SSA算法的无人机三维路径规划与多成本函数对比研究(Matlab代码实现)

内容概要:本文围绕基于多种智能优化算法(包括粒子群PSO、灰狼GWO、鲸鱼WOA、哈里斯鹰HHO、蜣螂DBO、麻雀SSA)的无人机三维路径规划展开系统性研究,提出了一套统一的三维环境建模与可行性约束体系,确保不同算法在同等条件下进行公平比较。研究通过Matlab代码实现了各类智能算法在复杂三维空间中的路径搜索过程,并针对多成本函数(如路径长度、飞行安全性、能量消耗等)构建综合评价指标,深入分析各算法在收敛速度、路径质量、稳定性和计算效率等方面的性能差异。通过对六种代表性群体智能算法的对比实验,旨在为无人机在复杂动态环境下的自主导航提供高效、鲁棒的路径决策方案。此外,文中还整合了丰富的科研资源与仿真案例,涵盖路径规划、电力系统、信号处理等多个领域,体现了较强的技术通用性与科研支撑能力。; 适合人群:具备一定Matlab编程基础和优化算法理论知识,从事自动化、人工智能、无人机控制、智能交通、机器人导航等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于无人机在复杂三维空间中的路径规划算法选型与性能评估;②作为智能优化算法在实际工程问题中应用的教学与科研参考材料;③支持科研工作者开展算法对比实验,推动路径规划算法的改进与创新;④为相关领域的仿真建模与系统优化提供可复用的技术框架。; 阅读建议:建议结合提供的Matlab代码和完整资源包进行实践操作,重点关注不同算法在相同环境与多目标成本函数下的表现差异,深入理解算法机制与参数调优策略,同时可将该研究范式拓展至其他智能算法或应用场景中进行进一步验证与优化。

销售述职体系建设方案(含流程与评分表).docx

销售述职体系建设方案(含流程与评分表)

老板演讲紧张怎么办之脱敏训练法.pdf

老板演讲紧张怎么办之脱敏训练法

12 《脱敏演讲训练感悟》 餐饮连锁老板 不再只会报菜名.docx

12 《脱敏演讲训练感悟》 餐饮连锁老板 不再只会报菜名

Bugku MISC zip伪加密(超详细题解 WP)

ZIP 伪加密:1、文件头的全局方式位标记为00 00;2、而目录区的全局方式位标记为09 00或01 00(奇数)

2301_81212117的博客 1368
上一篇: 10 Interview questions on Singleton Pattern in Java
下一篇: 深入理解java多态性
billdwl
博客等级 码龄20年 0粉丝 67原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值