




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Eclipse快速入门:远程调试Java应用2012年03月27日00:00 it168网站原创 作者:皮丽华 编辑:皮丽华 评论:0条 【IT168 技术】在软件开发中,调试是不可或缺的一个环节,它可以帮助开发人员提高效率,节省开发时间。在我认为,本地调试是最优选择,通常情况下,当无法实现本地调试时,我们才会考虑远程调试。有些项目是运行在Linux 操作系统上,而我们大多数项目是在Windows 平台上完成开发的。特别是在投资银行领域,很多电子交易程序运行在Linux 服务器上,这会让开发过程显得较为繁琐,因为我们没法在本地的开发环境中运行这些代码。有时候无论是出于开发还是调试的考虑,我们可以让项目运行在Windows 平台上,但有时候,一些不可控因素,比如您的项目需要提供开发库的平台,或者需要一些Linux模块,Windows平台上无法满足这些需求,或者您的项目规模太大,无法在Windows 平台上成功运行,由于项目过度依赖一些系统,没法在Windows 开发平台中创建相同的运行环境。这种情况下,我的解决方法是借助模拟的对象来测试项目。当然,有时候通过线程单独来运行应用程序模块并不见得是最好的解决方案,特别是您需要在运行时环境下通过调试项目来查找一些子问题,这些方法就会显得不太实用。Eclipse为我们提供了最有用的功能,称为“远程调试”。借助于远程调试,您可以在Windows 平台上调试Linux的运行情况,为了提高开发效率,非常有必要了解清楚如何安装远程调试,如何在Eclipse中实现远程调试。如何在Eclipse中安装远程调试1)在Eclipse中安装Java项目2) 选择项目,在“Run”菜单选项下选择“Debug Configurations”Remote debugging with Eclipse 13) 调试配置窗口将会弹出,选择左边的“Remote Java Application”图标,右键选择“New”。Eclipse remote debugging 24) 点击“New”以后,Eclipse会为您选中的项目创建远程Java应用配置。接下来的一步是为远程调试安装主机和端口。Remote debugging in Eclipse 35) 现在输入主机名和端口,如果你想从Eclipse中关闭运行在Linux平台上的 Java应用程序,请选中Allow termination of remote VM”复选框。6) 在调试之前,确保设置好Java进程的各项调试信息,同时确保在同一个主机和端口上监听Java进程,否则Eclipse无法正常建立连系。7) 点击“Debug”开始调试主机和端口。8)您也可以在Remote Java Application” 中,Debug Configurations菜单下选中您的项目,然后点击DEBUG。为了远程调试一个Java应用程序,确保JVM的调试选项如下:java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y suspend=y -jar stockTradingGUI.jar使用Java Debug Wire Protocol (jdwp) protocol ,我们可以让Java应用程序stockTradingGUI进入调试模式,监听端口为8001。suspend=y 时,Eclipse 将应用程序连接到指定调试端口后,应用程序才开始运行。有一点非常重要,先运行应用程序,然后连接它,否则Eclipse会抛出异常Failed to connect to remote VM. Connection refused”.提示一:在JVM DEBUG参数中,有一个参数叫suspend,它的取值有两个,“y”或者“n”,如果您刚开始就想调试的话,将参数设置为suspend=y,这样Eclipse会远程连接Java应用程序。如果你想先运行项目,然后连接Eclipse,那么可以将参数设置为suspend=n,这样的话,Java应用程序会正常运行,之后Eclipse会开始远程连接。提示二:使用启动脚本输入JVM调试参数,在脚本中使用变量isDebugEnabled或者REMOTE_DEBUG_PORT,当你想远程调试Java应用程序的时候,可以将这个变量导出。整个操作过程非常简单,只需要一次性的安装即可。提示三:如果你得到错误提示Failed to connect to remote VM. Connection refused或者Connection refused: connect,那么可能出现的原因有两种可能。第一是您的Java项目没有运行在远程主机上,第二是输入端口和主机名有误,验证成功后,可以填写完整的主机名。提示四:您也要确保本地Eclipse和远程Eclipse运行的是相同的代码库。使用调试选项-g可以编译代码,Eclipse可以轻松搜集调试信息,比如本地变量的调试信息,默认情况下,使用调试选项“-g”,类文件可能更大,因为它可能会包含一些调试信息。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 doesnt 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. Its also good to know various java debug tool available and how java debugger or jdb works but its 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 toolsI 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 optionsIf 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 PurposeXdebug Used to run java program in debug modeXrunjdwp: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 application1) 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 StockTradingAfter starting your java application in debug mode you can attach java debugger jdb to the VM with the following command:% jdb -attach 8000You can check the jdb manual page for complete detail on how to do java debugging with jdb.Java remote debugging with eclipseThis 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 NetbeansDebugging java application locally on any IDE like Eclipse or Netbeans its 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 tipsNow lets see some java debugging tips which I used while doing debugging in Java in eclipse.1) Use conditional breakpointEclipse 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 breakpointHow many times you have frustrated with a NullPointerException and you dont 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 IntoThese 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 ThreadThis 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 WatchThese 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 threadYou 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 structureLogical 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 filteringWhen 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 dont want to navigate into JDK System cl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2025学年度中医助理医师综合提升测试卷【达标题】附答案详解
- 2025年注册公用设备工程师综合提升测试卷附答案详解【夺分金卷】
- 自行车与摩托车课件
- 2024主管护师(中级)试题预测试卷及参考答案详解(考试直接用)
- 2024-2025学年证券从业资格考前冲刺练习题及完整答案详解(名校卷)
- 自治区百千万安全培训课件
- 导游资格考试复习提分资料(真题汇编)附答案详解
- 2024高职单招常考点试卷含答案详解【模拟题】
- 自我介绍课件教学模板
- 2024收银审核员通关考试题库附参考答案详解【培优A卷】
- 2025-2030中国软件外包行业市场发展分析及前景趋势与投资研究报告
- 大学生宿舍行为规范准则
- 地铁机电安装与装饰工程监理规划
- DB21T 4094-2025特色民宿建设与运营指南
- 工程监理质量评估报告
- Unit 2 My school things 第一课时 Get ready(教学设计)-2024-2025学年外研版(三起)(2024)英语三年级上册
- 专利知识培训教学课件
- 城市桥梁安全性评估规程DB50∕T 273-2021
- 数据库应用技术-第三次形考作业(第10章~第11章)-国开-参考资料
- 新能源汽车故障诊断试题库+答案
- 北京版(2024)小学一年级全一册体育与健康全册教案
评论
0/150
提交评论