10 Tips For Java Debugging Using Eclipse

Java debugging is an important part of programming in the language. You can use the Eclipse IDE for this purpose. It helps you find out where you have made a mistake and lets you keep the code clean

  1. Conditional Breakpoint: To add a breakpoint you just need to click on the left pane that is just before the line number. With respect to debugging, the 'Breakpoints' perspective will show you all the breakpoints that you have created. In addition, you can also add a Boolean condition, which ensures that a breakpoint is activated only if the Boolean condition is met.
  2. Exception Breakpoint: When you're using the breakpoints view, you will find a button that is labelled 'J'. This can be used in order to add breakpoints based on Java exceptions. This is useful in various cases. For example, if you want your program to stop and debug when it finds a NullPointerException, a breakpoint can be used.
  3. Watch Point: When you modify or access a chosen attribute, it will stop the program execution and start debugging. Here you can create a Watch Point by selecting a class variable in the Outline View and choosing Toggle Watchpoint from its context menu. This Watch Point will be seen in Breakpoints View.
  4. Evaluation (Display or Inspect or Watch): You can add a permanent watch on an expression or variable, which is then shown in the expressions view during debug. The Ctrl+Shift+d or Ctrl+Shift+I shortcuts will show the value of a selected expression or variable.
  5. Change Variable Values: The value of a variable can be changed during debug quite easily. Choose a variable and find Variables view. Then select the value, type and enter.
  6. Stop in Main: This option can be enabled from the Run/Debug Settings. Go to Edit Configuration and check the box next to 'Stop in Main'. This will half the execution of a Java program that starts with a main method at the first line itself.
  7. Environment Variables: Environment variables do not always need to be added from System properties. You can do this using the Edit Configuration dialog box as well.
  8. Drop to Frame: This feature allows you to return the control to any frame in the call stack while you are debugging. It will not reset the changes that have been made to the variables. This allows you to choose whichever stack level that you want and restart debug from there.
  9. Step Filter: Using the Step Into option for methods, you can be driven into the external libraries that you may not need. In order to avoid this, you can add filters in your preferences that will exclude certain packages.
  10. Step Into, Over and Return: This is actually the first things that you should learn in debugging.

    F5 - Step Into: Moves to next step. If next line has method, then goes into first line of method.

    F6 - Step Over: Moves to the next line unless there is a method call in the current line. If there is a method call in the current line then the method is invoked and then control is moved to the next line.

    F7 - Step Return: This will move the control to the line from which the method was originally invoked. It has to be called within the method for that though.

    F8 - Move to next breakpoint


Popular posts from this blog