Page 1 of 1

ECLIPSE DEBUGGER JAVA TUTORIAL Rate Topic: ***** 1 Votes

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 16 September 2010 - 01:56 PM

ECLIPSE DEBUGGER
JAVA TUTORIAL


CONTENTS
• I. INTRODUCTION
• II. WORKSPACE
• III. PROJECT
• IV. JAVA PACKAGE
• V. CLASS
• VI. ECLIPSE SDK SOURCE EDITOR
• VII. ECLIPSE DEBUGGER
• VIII. DEBUGGING EXAMPLE PROGRAM
• IX. EXAMPLE PROGRAM
• X. REFERENCES


I. INTRODUCTION

Hello; nice to meet you. Welcome to the “Java Eclipse Debugger Tutorial.”

This tutorial has been prepared using Microsoft Windows, Java SE Development Kit 6 update 14, and Eclipse Galileo version 3.5.2.

I tested this tutorial several times; however, if you find any errors or better ways of presenting the information please post them as tutorial comments.

II. WORKSPACE

Start Eclipse.

From the Java Eclipse SDK left click File and select Switch Workspace. This option shows you the path to your workspace location.

On my computer my Java workspace location is as follows:

C:\Eclipse Workspace



III. PROJECT

The Java project contains source code and related files for building a java program.

Perform the following steps to create a project using the Eclipse New Project Wizard:

A. From the Java Eclipse SDK click File.
B. From File select New.
C. From New select Project.
D. From the New Java Project wizard, at “Project name” type in your project name then accept the other defaults on this window by pressing Next. The example program Project name is frakal.
E. From the New Java Project wizard next window accept the default options by pressing Finish. Eclipse will create your project and return you to the Java - Eclipse SDK.

IV. JAVA PACKAGE

A Java Package contains related class files and allows you to use short-cut class names.

Perform the following steps to create a package:

A. From the Java Eclipse SDK Package tab window select the appropriate project folder where you want the Package created, right click the project folder and select New.
B. From New select Package to launch the Eclipse New Java Package Wizard.
C. From the New Java Package Wizard, at package “Name” type the package name. The example program package name is frakal.
D. From the New Java Package Wizard click Finish. The package will be created and you will be returned to the Java - Eclipse SDK.

V. CLASS

A class is a blueprint for creating objects.

Perform the following steps to create a class:

A. From the Java Eclipse SDK Package tab window select the appropriate package in the src folder, right click the package, and select New.
B. From New select Class to launch the Eclipse New Java Class Wizard.
C. From the Eclipse New Java Class Wizard, at “Name” type in a class name. The example program class name is frakal.
D. From the Eclipse New Java Class Wizard accept the default settings by selecting Finish. The class will be created and you will be returned to the Java Eclipse SDK.

VI. ECLIPSE SDK SOURCE EDITOR

A. Java Eclipse SDK Editor Window Font

If you want to change the Java Eclipse SDK Editor Window font to make the text easier to read perform the following steps:

A. From the Java Eclipse SDK Editor Window, left click window.
B. From Window left click Preferences.
C. From Preferences left click General.
D. From General left click Appearance.
E. From Appearance left click Colors and Fonts.
F. From Colors and Fonts left click Java Editor Text Font.
G. From Java Editor Text Font left click the Edit button.
H. From Java Editor Text Font select the font you prefer, for example 12 point and bold to make it easier to see.
I. From Java Editor Text Font left click OK.
J. From Java Editor Text Font left click Apply.
K. From Java Editor Text Font left click OK and you will be returned to the Java Eclipse SDK Editor window.

You can repeat these steps at any time and adjust the font to whatever size you need to make it easy to read.

B. Java Eclipse SDK Editor Window Size

To give you more space double left click on the tab at the top left of the Editor Window where your class appears and the Editor Window will expand to full screen to give you more screen work area room.

C. Editor Line Numbers

Perform the following steps if you would like to have source code line numbers:

A. From the Java Eclipse SDK Editor Window left click window.
B. From Window left click Preferences.
C. From Preferences left click General.
D. From General left click Editors.
E. From Editors left click Text Editors.
F. From Text Editors left click the box that says Show Line Numbers.
G. From Text Editors left click Apply.
H. From Text Editors left click OK and you will be returned to the Java Eclipse SDK Editor window.

At this point your Java Eclipse SDK Editor Window should contain something similar to the following based on the names you selected:

1 package frakal;
2 
3 public class frakal {
4 	
5 	
6 	
7 }
8 



Notice the first line is the “package statement” ending with a semicolon.

The third line is the “class statement.” The body of the class is placed between the two left and right curly braces following the class.

When you are debugging lots of line numbers, from the Debug perspective, you can left click Navigate, and left click “Go to Line ” to obtain the Go to Line pop up window. From the Go to Line pop up window enter the line number you want to go to, left click OK and you will go to that line number.

Copy the example program. Select the hi-lighted area of the class you just created, as shown below. Then paste the example program into the class you created.

[color="#00FFFF"]1 package frakal;
2 
3 public class frakal {
4 	
5 	
6 	
7 }
8 [/color]



VII. ECLIPSE DEBUGGER

A. Break Point

From the Java Eclipse SDK Editor Window double left click in the grey shaded margin to the left of a line number and you will see the Debugger Break Point circle icon appear. The icon is a toggle which can be turned on and off by double left clicking.

A break point suspends the thread execution of the source code before the line containing the break point is executed.

B. Debug

After you set your break point use the following steps to run debug:

1. From the Java Eclipse SDK Editor Window left click Run.
2. From Run left click Debug.
3. The Confirm Perspective Switch pop up window will appear. From the Confirm Perspective Switch pop up window left click Remember My Decision, then left click Yes and you will be in the Java Eclipse SDK Debug Perspective. The next time you use Debug the Confirm Perspective Switch pop up window will not appear, you will go directly to the Java Eclipse SDK Debug Perspective.

You will discover there are several ways to enter Debug and of course you can use what ever method you prefer. For example, from the Java Eclipse SDK Editor Window you can enter Debug by also doing one of the following:

1. Left click Window, then left click Open Perspective, and then left click Debug.
2. Left click Debug on the tool bar at the far right of the top of your screen.
3. Press F11.

C. Java Eclipse SDK Debug Perspective

The Java Eclipse SDK Debug Perspective has several tab windows, each of which has buttons. Hovering your mouse pointer over a button will reveal to you the purpose of that button.

1. Debug shows the call stack and status of all current threads, including any threads that have already run to completion, and the line of code the debugger is currently executing.

The following buttons for moving around within the program are in the title bar of the Debug view tab:

a. Step Into, F5, is used to Step Into a method and stop at the first line of executable code inside that method.

Normally you only want to step into methods in your own classes and not into the third-party packages or standard Java packages. In order to accomplish this left click Window, left click Preferences, left click Java, left click Debug, left click Step Filtering, left click Select All, left click Apply, and left click OK.

b. Step Over, F6, executes the current line of code and stops at the next line.

c. Step Return, F7, executes everything within a method entered by using Step Into and then returns to the next line following the line which contained the method we previously stepped into.

d. Resume, F8, will resume execution of the program until it meets the next breakpoint.

Another way of moving around within the program is to right click on the line you want the program to run to, then select Run To Line or use CTRL+R. The program will run to the line you selected and stop.

2. Java Editor (Source Code Window)

The Java Editor shows the Java source code. When Debug executes the program pauses at the first executable line on or after the breakpoint.

3. Console

Console shows the programs console output.

4. Tasks

The Tasks view displays the list of to-do items, or tasks that you have added manually.

5. Variables

Variables shows the current variables and their values.

6. Breakpoints

Breakpoints lists all the breakpoints. You can either disable or enable the breakpoints by checking or un-checking the check mark next to each label.

7. Outline

Outline lists an outline of the current source file.

8. LogCat

The LogCat messages give you the full breakdown of what caused the error, including the line number.

If you X out of (close) a perspective tab window and you want to see it again, left click Window, then left click Show View, then left click the perspective tab window you closed and Eclipse will show it again.

For example, when you first enter perspective, you will not be able to view the perspective tab window Expressions tab until you left click Window, then left click Show View, then left click Expressions. You can use the Expressions tab to have the Debugger evaluate and keep track of expressions.

When you want to exit the debugger, refer to the top of the Java Eclipse SDK Debug tab window tool bar, and left click the red button. Then left click Window, left click Open Perspective, and left click Java to return to the Java Eclipse SDK Editor window.

VIII. DEBUGGING EXAMPLE PROGRAM

The example program is a fractal. I think fractals are fantastic. I deliberately included this fractal as an example program in order to give you a more challenging opportunity to practice debugging. Complex ideas involve lots of user created methods and lots of open source utility methods. I will cover enough in this section of the tutorial to get you started. If you have not previously looked at open source utility methods this will be an eye opener for you. If you have problems post them as comments and I will reply to your comments.

A. Break Points

Go to the main method and set a break point at the following line:

new fraktal(args);



After you have the break point set, from the main tool bar left click Run, then left click Debug and the program will run and pause at the line of code where you set the break point. The debugger has paused the program at the break point before executing “new fractal(args);”.

B. Step Into

From the Break Point, left click Step Into and you will enter the fractal(args) method for debugging. When you left click Step Into the debugger will Step Into a method and stop at the first line of executable code inside that method. In this case it has stopped at the following line of code:

super("Mandelbrot set fractal");



C. Step Return

Left click Step Return and you will return to the next line following the line which contained the method we previously stepped into. Our main method only contains one method. Therefore, when you left click Step Return the program creates the “Mandelbrot Set Fractal” and returns to the right curly brace which ends the body of the main method as shown below.

	}



I hope you like the fractal, I think it is very pretty. Close the “Mandelbrot Set Fractal” and left click the red terminate button in the Debug view title bar.

From the Java editor source code window set a second break point at the first for statement below the main method on the following line.

for (int i = 0; i < 50; i++)



Then from the top tool bar left click Run, then left click Debug and the program will run and pause at the line of code where you set the first break point.

From the Break Point, left click Step Into and you will enter the fractal(args) method for debugging.

D. Step Over

Now that we are in the fractal(args) method we can walk through the lines of code in the method by left clicking Step Over. Each time we left click Step Over, the debugger executes the current line of code and stops at the next line as shown below.

setSize(800, 800);



E. Resume

When we left click Resume or press F8 the Eclipse Debugger will resume execution of the program until it meets the next breakpoint. Therefore, when we left click Resume we arrive at our second break point.

Left clicking Step Over places us inside the for loop.

The for loop has 50 iterations. If we want to continue using Step Over and we prefer not left clicking Step Over 50 times, we can go to the Variables tab, find the variable i which increments the for loop, and change the value of i in the Value column. Then we can continue left clicking Step Over.

F. Watch

In the for loop there are variables whose value changes you might want to watch. To do so left click on the variable, then right click and select Watch. The variable you selected for watching will appear in the Expressions tab window where you can watch them change as you Step Over statements.

IX. EXAMPLE PROGRAM

/*
	Java
	Eclipse Debugger
	Tutorial
*/

package fraktal;

import javax.swing.*;       //  JPanel
import java.awt.*;          //  Graphics
import java.awt.image.*;
import java.awt.event.*;

  public class fraktal extends JFrame implements MouseListener
{
	BufferedImage fractal;
	WritableRaster raster;
	Calculator calc;
	int mouse_x, mouse_y;	
	double x;
	double y;
	double width;
	double height;
	
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited (MouseEvent e) {}
	public void mousePressed(MouseEvent e)
	{
		mouse_x = e.getX();
		mouse_y = e.getY();		
	}
	
	public void mouseReleased(MouseEvent e)
	{
		double tmpx = translateX(mouse_x);
		double tmpy = translateY(mouse_y);
		double tmp  = translateX(e.getX());
		x = tmpx;
		y = tmpy;
		
		width  = Math.abs(tmp - tmpx);
		height = 0.75 * width;
		while (!calc.isDone())
		{
			try { Thread.sleep(1000); } catch (InterruptedException ex) {}
		}
		
		calc = new Calculator(800, 800, raster, this);
		
		calc.setInsets(x, y, width, height);		
		
		calc.start();
	}
	
	public double translateX(int ix)
	{		
		return x + (((double) ix) / 800) * width;
	}
	
	public double translateY(int iy)
	{
		return y + (((double) iy) / 800) * height;
	}
	
	public fraktal (String args[])
	{
		super("Mandelbrot Set Fractal");
		setSize(800, 800);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		setResizable(false);

		Component graphics = new Component()
		{			
			public void paint(Graphics g)
			{
				g.drawImage(fractal, 0, 0, null);
			}						
		};
		
		graphics.addMouseListener(this);
		getContentPane().add(graphics);
		
		fractal = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
		raster  = fractal.getRaster();
		
		System.out.println("Raster top left corner: " + raster.getMinX() + "," + raster.getMinY());
		System.out.println("Raster size: " + raster.getWidth() + "," + raster.getHeight());
		
		setVisible(true);
		
		calc = new Calculator(800, 800, raster, this);		
		
		x = -1.5;
		y = -1;
		width  = 2;
		height = 0.75 * width;
		
		if (args.length == 0)
		{			
			calc.setInsets(-1.5, -1, 2, 0.75 * 2);
		}
		
		if (args.length == 3)
		{
			x = Double.parseDouble(args[0]);
			y = Double.parseDouble(args[1]);
			width  = Double.parseDouble(args[2]);
			height = 0.75 * width;			
			calc.setInsets(x, y, width, height);
		}
		System.out.println("Rectangle calculation: ("+x+","+y+") to (" + (x + width) + "," + (y + height)+")");
		
		calc.start();
	}
	
	/*
	 	Main Method 
	 */
	
	public static void main(String args[])
	{
		new fraktal(args);
	}
}

class Calculator extends Thread
{
	WritableRaster raster;
	int sizex;
	int sizey;
	
	double startx;
	double starty;
	double width;
	double height;
	double z;
	double zi;
	int[] black;
	int[] red;
	Object[] colors;
	JFrame parent;
	boolean done;
	 
	public Calculator(int sx, int sy, WritableRaster r, JFrame p)
	{
		parent = p;
		raster = r;
		sizex = sx;
		sizey = sy;
		z  = 0; 
		zi = 0;
		black = new int[3];
		black[0] = 0;
		black[1] = 0;
		black[2] = 0;
		red = new int[3];
		red[0] = 255;
		red[1] = 0;
		red[2] = 0;
		
		done = true;
		
		colors = new Object[200];
		int c = 255;
		for (int i = 0; i < 50; i++)
		{
			int[] color = new int[3];
			color[0]  = 0;
			color[1] = 255 - c;
			color[2] = c;
			colors[i] = color;
			c -= 5;
		}

		c = 255;
		for (int i = 50; i < 100; i++)
		{
			int[] color = new int[3];
			color[0]  = 255 - c;
			color[1] = 255;
			color[2] = 0;
			colors[i] = color;
			c -= 5;
		}

		c = 255;
		for (int i = 100; i < 150; i++)
		{
			int[] color = new int[3];
			color[0]  = c;
			color[1] = c;
			color[2] = 255 - c;
			colors[i] = color;
			c -= 5;
		}
		
		c = 255;
		for (int i = 150 ; i < 200 ; i++)
		{
			int[] color = new int[3];
			color[0]  = 255 - c;
			color[1] = 0;
			color[2] = 255;
			colors[i] = color;
			c -= 5;
		}
	}
	
	public void setInsets(double sx, double sy, double w, double h)
	{
		startx = sx;
		starty = sy; 
		width  = w;
		height = h;
	}
		
	public static double comp_mult_real(double a, double b, double c, double d)
	{
		return (a * c) - (b * d);
	}
	
	public static double comp_mult_imag(double a, double b, double c, double d)
	{
		return (a * d) + (b * c);
	}
	
	public int mandelbrotTest(double a, double bi)
	{
		
		double atmp;
		double btmp;
		int number = 0;
		double z  = 0;
		double zi = 0;
		
		while ( (number != 200) && (comp_magnitude(z, zi) < 2))
		{			
			number++;
			atmp = comp_mult_real(z, zi, z, zi);
			btmp = comp_mult_imag(z, zi, z, zi);
			z  = atmp;
			zi = btmp;
			z  += a;
			zi += bi; 			
		}
		
		if (number == 200)
		{
			return -1;
		}
		else
		{
			return number;
		}
	}
	
	public boolean isDone()
	{
		return done;
	}
	
	public static double comp_magnitude(double a, double B)/>
	{
		return Math.sqrt( a * a + b * B)/>;
	}
	
	public void run()
	{
		double dx = width  / sizex;
		double dy = height / sizey;
		
		double z  = startx;
		double zi = starty;
		
		done = false;
		
		System.out.println("Mandelbrot set fractal calculation is in progress.");		
		for (int x = 0; x < sizex; x++)
		{			
			zi = starty;
			int it;
			for (int y = 0; y < sizey; y++)
			{
				if ((it = mandelbrotTest(z, zi)) != -1)
				{
					// Within mandelbrot set.
					raster.setPixel(x, y, (int[]) colors[it]);					
				}
				else
				{
					// Not within mandelbrot set
					raster.setPixel(x, y, black);
				}
				zi += dy;
			}
			if ( (x%5) == 0)
			{
				parent.repaint();	
			}
			z += dx;
		}
		
		done = true;
		
		System.out.println("Mandelbrot set fractal calculation has completed!");
	}
}



X. REFERENCE

Java How To Program Eighth Edition by Paul Deitel and Harvey Deitel (Upper Saddle River, N.J.: Pearson Education, Inc., 2010).

Is This A Good Question/Topic? 0
  • +

Page 1 of 1