Eclipse can be used to debug your java applications. Here are a few tips that can help you debug better:
1. Remote Debugging The eclipse IDE can remote debug your web application. Imagine being able to debug your development server from your local machine. Eclipse can help you do this. First you need to instruct your application / server to listen on a port for debug messages. That can be done using the -Xdebug flag -
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8787,suspend=n
This tells the JVM to listen for specific debug messages on port 8787.
In eclipse open Bug Icon -> Debug configurations and select “Remote java application”. Mention the port and server address and you should now be able to remote debug the application. Make sure that the classes and source sync with each other. On a network where the bandwidth is poor, this technique will not work.
2. Use the display view: The debug perspective in eclipse can open up a “Display” view that allows you to dynamically enter some code for execution when you hit a breakpoint. This view is priceless. You can dynamically execute code related to the variables that are in scope. Use the icons available on the top right hand corner of the view to do this.
3. Set breakpoints at different levels: You can set class level, method level or line level breakpoints. Nothing special but once again it can save you time. Just set a breakpoint corresponding to a class declaration a method declaration or a specific line.
Set conditional breakpoints: Breakpoints can be triggered conditionally. Simple right click on a breakpoint and select “Breakpoint properties”
There are 3 settings that you can tweak.
- Hit count – Break only when code flow passes over this breakpoint N times.
- Condition – Break when a specific programmatic condition is reached.
- Suspend VM Vs Suspend Thread – This is useful when debugging multithreaded application. If you want to suspend only the current thread and not all threads that are running through this code flow, you can use this drop down.
4. Specify a detail formatter of your own: Use the Variables view to mention a detailed formatter for an object type. This will display the object in question in a different format when you look it up under Variables or in an inspection window.
5. Use inspection windows: Inspection windows can reveal the detailed object graph of the instance in question. Simply highlight the variable and type the shortcut ctrl+shift+i
6. Use all instances / references: Use the all instances / all references option in the Variables view to determine the usage of a particular Object type at runtime. Arguably a profiler might be better suited for this sort of work, but hey, you never know when you might need it.
7. Change variable values dynamically: You can use the Variables view to change the value of a variable dynamically. This helps to determine code flow control if you have conditional statements. You could also do the same using the Display view but the GUI helps.
8. Use the drop to frame feature: The “drop to frame” feature allows you to retrace your path after making a minor code change. The code flow goes back to the line of code you have dropped to and executes once again. You could even, say, change a database variable and drop back a few call stacks behind to retrace your steps.
That’s about the list of debugging tips I know. Do you have one that could help other developers ? Drop a comment.