Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Getting Started with the Eclipse Workbench : Page 4

If you think an open source product like the Eclipse Workbench is necessarily Spartan and unassuming, think again. With a collection of great UI features, Eclipse may be good enough to make you wrinkle your nose at your current IDE. Get a look at what you're missing.

Creating a Java Class
The Eclipse JDT includes a special incremental compiler and evaluates your source code as you type it.
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. Here, we'll use org.eclipseguide. To this, add a name for this particular project: hello. All together, the package name is org.eclipseguide.hello.

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 3.
  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.

Figure 3. Hello World: the HelloWorld class using the New Java Class Wizard is shown.
In the editor area in the middle of the screen, you see the Java code generated by the wizard. Also notice that tabs now appear at the top of the editor area, which allow you to select between the Welcome screen that first appeared and this new HelloWorld.java file. (You don't need the Welcome screen anymore, so you can click on the Welcome tab and click the X in the tab to make it go away.) You may also want to adjust the size of your windows and views to get a more complete view of the source code and the other views.

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 do this, alter the code generated by Eclipse by adding a line to main() as follows:

/* * Created on Feb 14, 2003 * * To change this generated comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ package org.eclipseguide.hello; /** * @author david */ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }

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.

For most simple programs, you don't need a special launch configuration, so you can use a much easier method to start the program: First make sure the Hello World source is selected in the editor (its tab is highlighted in blue) and then do the following from the Eclipse menu:

  1. Select Run > Run As > Java Application.
  2. Because you've made changes to the program, Eclipse prompts you to save your changes before it runs the program. Click OK.
  3. The Task view changes to a Console view and displays your program output (see Figure 4).
Figure 4. Displaying the Output: The Eclipse Console view displays the output from the HelloWorld program.
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.

The key to using Eclipse effectively is to understand its organizational concepts of perspectives, views, and editors. The different views that appear at one time on the Workbench, which together are called a perspective, are especially selected to enable you to accomplish a specific task, such as creating a Java project, working with source files, and running a program.

David Gallardo is an independent software consultant specializing in software internationalization, Java web applications, and database development. His recent experience includes leading database and internationalization development at a business-to-business e-commerce company, TradeAccess, Inc. He was also a senior engineer in the international product development group at Lotus Development Corporation, where he contributed to the development of a cross-platform library providing Unicode and international language support for Lotus products including Notes and 1-2-3. He is the author of Java Oracle Database Development.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date