Article published by : Infocampus training institute on Thursday, January 04, 2018 - Viewed 129 times

Category : Software

Debugging a Java Program with Eclipse



A guide for Java developers provides an introduction to creating a Java project, running a Java program, and debugging it.
Creating a Java Project
When Eclipse is first started, it is in the Resource perspective, a set of panes for managing projects, folders, files, and other resources. In Eclipse parlance, each of these panes is called a view, and a complete set of views is called a perspective. In the Resource perspective, you can navigate or create resources by using the view at the top left, the Navigator view.
Before you can do anything else in Eclipse, such as creating a Java program, you need to create a Java project. To create a new Java project, follow these steps:
1. Right-click in the Navigator view to bring up a context menu and select New->Project.
2. In the New Project dialog box, Eclipse presents the project options: Java, Plug-in Development, and Simple. Because you want to create a Java program, select Java on the left side of the dialog box.
3. Select Java Project on the right. If you've installed other types of Java development plug-ins, various other types of Java projects may potentially be listed here (EJBs and servlets, for example). But the JDT that comes standard with Eclipse only offers support for standard Java applications, so you must choose the Java Project option.
4. Click Next to start the New Java Project Wizard. (A wizard is a set of dialog boxes that prompts you through a set of well-defined, sequential steps necessary to perform a specific task. This feature is used extensively throughout Eclipse.)
5. The first dialog box prompts you for a project name. This is a simple "Hello, world" example, so enter Hello. Clicking Next would take you to a dialog box that lets you change a number of Java build settings, but for this example you don't need to change anything.
6. Click Finish.
7. Eclipse notifies you that this kind of project is associated with the Java perspective and asks whether you want to switch to the Java perspective. Check the Don't Show Me This Message Again box and click Yes.
Creating a Java class
Once you've created a project for it to live in, you can create your first Java program. Although doing so is not necessary, it's a good practice to organize your Java classes into packages. It's conventional to use a domain name as the package name because this reduces the likelihood of name collisions—that is, more than one class with exactly the same name. You can use a registered domain name if you have one, but if not, you can use any convenient, unique, ad hoc name, especially for private use.
Follow these steps to create your Java program:
1. Right-click on the project and select New.Class to bring up the New Java Class Wizard.
2. The first field, Source Folder, is by default the project's folder—leave this as it is.
3. Enter org.eclipseguide.hello in the Package field.
4. In the class name field, enter HelloWorld.
5. In the section Which Method Stubs Would You Like to Create?, check the box for public static void main(String[] args). The completed New Java Class dialog box is shown in Figure 1.
6. Click Finish, and the New Java Class Wizard will create the appropriate directory structure for the package (represented in the Navigator by the entry org.eclipseguide.hello under the Hello project) and the source file HelloWorld.java under this package name.


The code that's automatically generated includes a method stub for main(). You need to add any functionality, such as printing your "Hello, world!" yourself. To make debugging more interesting, you'll add a separate method with a loop to print. Alter the code generated by Eclipse as follows:

Running the Java Program
You're now ready to run this program. There are several things you might want to consider when running a Java program, including the Java runtime it should use, whether it will take any command-line parameters, and, if more than one class has a main() method, which one to use. The standard way to start a Java program in Eclipse is to select Run->Run from the Eclipse menu. Doing so brings up a dialog box that lets you configure the launch options for the program; before running a program, you need to create a launch configuration or select an existing launch configuration.
You may wonder why no separate step is required to compile the .java file into a .class file. This is the case because the Eclipse JDT includes a special incremental compiler and evaluates your source code as you type it. Thus it can highlight things such as syntax errors and unresolved references as you type. (Like Eclipse's other friendly features, this functionality can be turned off if you find it annoying.) If compilation is successful, the compiled .class file is saved at the same time your source file is saved.
Debugging the Java Program
Eclipse's ability to run the code interactively is one of its most powerful features. By using theJDT debugger, you can execute your Java program line by line and examine the value of variables at different points in the program, for example. This process can be invaluable in locating problems in your code.

---
NationDrugs.com


Keywords: java/j2ee classes bangalore, best java training institute bangalore Marathahalli, Java Training in Bangalore

By: Infocampus training institute

Article Directory: http://www.articlecatalog.com

Copy and Paste Link Code:


Read other Articles from Infocampus training institute: More »

Article ID 1055539 (Views 129)



Sponsor Listing

SNationDrugs.com Canadian Pharmacy