Debugging Stand alone code using Eclipse

Debugging stand alone code using eclipse

Users who are very much new to java and its development and they want to know how to trace the code and its behavior then I recommend to use Debug Mode while using Eclipse IDE.

If we are using Debug Mode then we need to change the Open Perspective from Java or J2ee to Debug option.

Debug mode will give us clear idea like how code flow is going on and what is value currently step by step.

If we need to Debug few values at specific line of code we need to enable break points so that Debug will wait there and show you status with every individual variable value.

Now we will try to debug a very simple code snippet.

package test;

public class HelloWorld {

       * @param args
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("Hello World !!!!");

            HelloWorld hello = new HelloWorld();
            System.out.println(hello.addValues(10, 15));
      public void sayHello(){
            System.out.println("Current I am in sayHello method");
      public int addValues(int a,int b)
            System.out.println("Currently I am in addValues Method");
            System.out.println("Value of a "+a);
            System.out.println("Values of b "+b);
            return (a+b);


Now we need to set break point where ever we feel we need to verify the exact values.

Below are few break points which set.

Now how to set these breakpoints:

Select specific line of the code where we want to set toggle breakpoints and double click there or select left side border opposite to required line and right click and select “Toggle BreakPoint”.

Now we are all most ready to compile and execute the code in Debug mode.

Select bug icon and press required Class in our case we are using HelloWorld or we can select Menu as Run >> Debug As >> <Specific Class Name>

When we request to run the Java class under Debug Mode it will check for confirmation to switch Perspective.

Once we press yes it will change to the Debug Mode as Open Perspective type by itself. 

Left top pallet is used to check the Toggle Breakpoints and its sequence Right top pallet is used to check the value of every Variable, Middle left Pallet is actual code where if we see light green shaded line shows where current Debug cursor is currently.

Below we see console Pallet which are used to share output once we execute the Debug code.

If we check this figure  two options are there first option is "Step into" or "press F5" or second option is  "Step Over"  or "press F6" to move sequence of the debug

So Debug will share us step by step and sequence of every line of code.

So please try this option to understand movement of Java code.

Happy coding :) 

No comments:

Post a Comment