Introduction to the Eclipse Development Environment – Creating a First Java Project

Abstract

In this lab, you will learn a bit about the Eclipse development environment and how to create a first Java program by doing the following:

  • Create a first Java project within Eclipse
  • Add a Java class to the project
  • Compile and run a Java project
  • Explore selected Eclipse windows (Views)
  • Debug a Java project using the Eclipse debugging tools
  • Export a Java project so that you can see how to move them from computer to computer

Key Terms

  • An Eclipseperspective is a visual container for a set of views and editors (windows). One perspective is visible at a time. Each perspective provides functionality to accomplish a particular task, such as editing or debugging a Java or Android program. The current perspectives appear along the top right side of the toolbar as follows. In this figure, the Java perspective is active (shaded)

    In this course, you will commonly use the following perspectives:
  • The Java perspective is designed for editing Java (and Android) projects. It has an editor area and hasviews (windows), used to manage a project.
  • The Package Explorer operates similar to the Visual Studio Solution Explorer.
  • The editor area works similar to the .NET Code Editor.
  • The Debug perspective is designed todebug Java programs. It includes an editor area in which you examine a program’s code, and the following views: Debug, Breakpoints, Variables, Expressions, Outline, Console, Tasks. These views allow you to suspend program execution, and then look at the values of variables and program objects. Again, these debugging tools have the same purpose and work the same way as they do in .NET.
  • Eachperspective has one Editor Area (visible region), but multiple editors can be open concurrently. In an editor area, you change the content of programmatic data files such as XML or Java code.
  • Resources is a collective term for the projects, folders, and files that exist in the Workbench.
  • Views support editors and some have their own toolbars. A view might appear by itself or stacked with other views in a tabbed notebook. Eclipse has hundreds of different views. Only selected views will be discussed in this lab.The terms View and window mean roughly the same thing.
  • The Workbench refers to the Eclipse desktop development environment. Each Workbench window contains one or more perspectives. One perspective in a Workbench is visible at a time.

Introductory Eclipse Notes

Eclipse is an integrated development environment (IDE) designed for developing programs in Java and other programming languages such as Python, PERL, and Ruby, to name a few.

Eclipse contains:

  • A development platform with which you create, compile, test, and debug applications written in one or more programming languages, and targeted to be executed on one or more hardware and software platforms.
  • Extensions called plug-ins that supporta particular programming language or platform. For example, SAP and others use Eclipse as a development platform for their software. See

Creating a First Java Project with Eclipse

In this part of the lab, you will create a first Java project. The process is similar to creating a project in Visual Studio .NET.

  • You create a project having a particular type, such as a Java or Android project. From that project type, Eclipse uses a template to create a working application from a template.
  • You specify various configuration options for each type of project. Eclipse uses this information to create the default application (project) from its underlying template.

HANDS-ON ACTIVITY – Creating a Java project

  1. Start Eclipse.
    When you start Eclipse, you will be asked to select a default workspace. This default workspace is where all of your applications (projects) are stored. I suggest that for all exercises and programs in this course, that you use a default workspace on the same USB stick. This way, you can more easily move the projects you create to different computers. You might also consider creating a folder in your StudentHome folder.
  2. On the menu bar, click File, New, Java Project. If the Java Project menu does not appear, click File, NewProject, and select JavaProject from the following dialog box. Note that you will need to expand the Java folder.

  3. In the following dialog box, give the project a name. I suggest that you use the name HelloJava. Do not put spaces in project names.
    Note the following about the information inthe following dialog box:
    In the JRE section, the option Use an Execution environment JRE defines version of the Java runtime that will be used to execute the program.
    In the Project layout section, the option Create separate folders for sources and class files causes the Java application to be stored in multiple folders making it easier to organize larger applications.

  4. Click Next to begin creating the project. The New Java Project dialog box appears. You should not need to change any of these options. However, it is here that you can control where Eclipse will store source files, and any external libraries needed by the project.

  5. Click Finish to create the project. When you do, the Eclipse development environment appears and the default Perspective is activated.

Selecting a Perspective

Recall that Eclipse organizes Views into a Perspective and one Perspective is visible at a time. When you develop Java code, you would usually use the Java Perspective. When you debug Java code, you would use the Debug Perspective. In the steps that follow, you will look at these two perspectives:

HANDS-ON ACTIVITY – Selecting Eclipse Perspectives

  1. ClickWindow, Perspective, Open Perspective, Other. Click Java from the dialog box that appears.
    As you work with Eclipse, you will likely open and close various windows (Views) within a Perspective.To get back to the default configuration for the current perspective, click Window, Perspective, ResetPerspective. In the dialog box that appears, click Yes to reset the perspective to its defaults. Your screen should look like the following with the Java Perspective selected with the default View:

Note the following about the above window:

  • The PackageExplorer appears docked along the left margin. It shows a single package named HelloJava. If you expand this folder, you will see the files that comprise the package. The Package Explorer works similar to the Visual Studio Solution Explorer. It lists all of the packages in the selected Workspace. In the above window, there is a single package. However, a Workspace will often have several packages.
  • Along the bottom of the window appears three views named Problems, Javadoc and Declaration. These three views appear in a View folder. (Really just a set of tabs). The Problems View lists syntax and other errors recognized as you develop a program’s code. The Outline View shows an outline of a structured file, such as a Java class.
  • You will not use the Outline or Task View in this session.

Adding a Java Source File to the Project

At this point, you have an empty program with no source code.Next, you will add a Java source file to the package.The process is similar to the process of adding a form to a VB project. This source file has the following characteristics:

  • Java is an object-oriented programming language so everything is a class. Classes, in turn, have methods (functions) and properties (data / variables).
  • All Java programs have what is known as an entry point. A Java program’s entry point is where a program begins execution. A Java program’s entry point appears in one of its classes. A program cannot have more than one entry point.
  • A Java program’s entry point is always a procedure named main and has the following signature (name, data type, and arguments):

public static void main(String[] args)
{
}

Classes will be discussed in much more detail later in the Java Tutorial. The syntax and purpose of the above code will also be discussed in a moment.

HANDS-ON ACTIVITY – Creating a main procedure (entry point)

  1. Make sure that the Package named HelloJava is selected in the Package Explorer.
  2. On the menu, click File, New, Class to display the following dialog box:
  3. Set the Name to HelloClass. Note that class names cannot contain a space and must begin with a letter. They can contain an underscore but other special characters are prohibited.
  4. Make sure that the Modifier is set to public. Access modifiers will be discussed later in this document.
  5. Check the box titled public static void main(String[] args). Checking this box causes Eclipse to create the program’s entry point as you will see in a moment.
  6. Click Finish to create the class.

    Eclipse creates the class and entry point as shown in the following figure:

Note the following about the above code:

  • There is a class (declared with the class keyword) named HelloClass. In this context, a Java class is no different than a VB class.
  • The class is declared with the public access modifier indicating that it can be referenced (used by) other packages.
  • The {} characters mark the beginning and end of the class. These characters mark a statement block for all classes and procedures.
  • In Java, all blocks are marked with the {} characters. Inside of the class, there is a single method (function) named main, which is the program’s entry point.
  • The line beginning with the two forward slashes (//), is a comment and is ignored by the compiler.
  • The keyword public indicates that this class and function can be used by other packages. The keyword has the same meaning in both Java and Visual Basic.
  • The static keyword indicates that the method is a static method rather than an instance method. Static methods are called without first creating an instance of the class.The program’s entry point (main procedure) is always a static procedure.
  • The keyword void indicates that the method does not return a value. void procedures are similar toSub procedures in VB.
  • String[] args is an argument. The argument is an array of strings. In the case of the main procedure, this argument provides the means to pass data from the command line to the procedure. In this program, you will not work with command line arguments.
  • The {} marks the boundaries of the procedure.
  • Note that procedures cannot be nested inside each other.

All of this syntax will be discussed in more detail as you progress through the tutorial.

Writing Code to Display Output

In this part of the lab, you will write a statement to display the string “Hello”. In these first examples, you will use a View(window) called the Console View, instead of displaying output to user interface controls. The reason is simple. Developing a Windows / Java user interface requires that you learn how to use another tool called Swing. Your goal here is to learn the basics of Java, rather than learn how to create user interfaces in Java. As you progress through the course, you will create Android user interfaces, rather than creating Java user interfaces.

The following statement will write the output string “Hello” to the Console View:

System.out.println(“Hello”);

Note the following about the above statement:

  • System is a class built into the Java language. Just as .NET has many built-in classes. So too do does Java.
  • out is a public static member of the System class. While not obvious, it has a data type of PrintStream. By public, we mean that this member is accessible by any other class.
  • The overloaded println method of the PrintStream class accepts one argument. In this case, the string to print to the Console View. When executed, the println method writes its string argument, followed by a carriage return. Note that the print method does the same thing but does not write the carriage return. Literal string values are enclosed in double quotation marks.
  • All Java statements are terminated by a semicolon (;). Procedure declarations and statement blocks ({}) are not terminated by semicolons.

HANDS-ON ACTIVITY – Writing the Hello code

  1. Make sure that the Editor for the HelloClass.java file is active. If it is not, double-click the class name in the Package Explorer.
  2. Enter the following statement inside of the mainprocedure. Remember that Java is case sensitive, and that the statement must end in a semicolon.
    System.out.println(“Hello”);
  3. Click File, Save All to save the file.

Compiling and Running the Program

Now that this first simple Hello program is written, you can compile and run it. Java is a compiled language, rather than an interpreted language. Programs with syntax errors cannot be compiled and therefore cannot be run. Syntax errors are marked in the Problems View.
HANDS-ON ACTIVITY – Compiling and executing the program

  1. Next, the project needs to be compiled into an executable program. This process will vary slightly based on your Eclipse configuration.
    On the menu, if the Project, BuildAutomatically check box is checked, you will be prompted to save files before the project is run. In addition, the project will be recompiled before the project is run.
    If the Build Automatically check box is not checked, click Project, Build Project to compile the project.
    The Problems View should be visible along the bottom of the Eclipse window. Activate that View to make sure that there are no syntax errors. If there syntax errors, they will be listed here.

  2. Make sure the HelloJava project is selected in the Package Explorer.
  3. Next, you can run the application. Click Run, Run As, Java Application. The output should appear in the Console View as shown in the following figure:

Creating a Syntax Error

When a statement violates the rules of a programming language, a syntax error is said to have occurred. In the following set of steps, you will intentionally create a syntax error to see how Java flags it for correction:

HANDS-ON ACTIVITY – Creating and Fixing a Syntax Error

  1. After the correct statement you just wrote, enter the following invalid statement. Note that printline is not a method of the System.out class so it is marked as having a syntax error. Note that the syntax error(s) is flagged in the Editor’s left margin with the red X. The offending part of the statement is also displayed with a jagged red underscore.

  2. Try and build the program again by clicking Project, Build Project, the syntax errors are listed in the Problems View as follows:

    As you can see, there are two syntax errors. The first indicates that the semicolon is missing from the end of the statement. The second indicates the printline is not a method. Both errors occurred on line 8. Your line numbers might vary.
  3. Delete the line containing the errors and rebuild the program.

Writing Code to Get User Input

In addition to displaying output, you must be able to get input from the user. There are several ways to get console input from the user. With the current version of Java, the easiest way is to use the Scanner class. Examine the following statements that show how to get input using the Scanner class.

Scanner = new Scanner(System.in);
String userName = scanner.next();

  • The first of the above statements creates an instance of the Scanner class. The new keyword calls what is called the constructor. As in VB, constructors create objects from classes. One argument (named System.in) is passed to the constructor. The System.in class works similar to the System.out class. Instead of writing output to the Console View, it reads input from the Console View. The system.in class is predefined and is always available to your program.This statement creates the Scanner and wires it to read input from the Console View.
  • The next statement (scanner.next())reads an input line from the Console View and stores the result in the variable username. next is an instance method of the scanner class. Data types will be discussed later but variables of the String data type store a string of characters.

HANDS-ON ACTIVITY – Reading user input
In this next set of steps, the code that you write will produce another syntax error. This is intentional so that you can see the purpose of the Java import statement and how to use the Eclipse intellisense tools to fix the problem.

  1. Modify the code that you just wrote so that it looks like the following. Note that the statement to create the Scanner object has a syntax error at this point.

  2. Move the mouse so that it hovers over the syntax error. The syntax error is marked with the jagged red lines. The following tooltip should appear.

    Note that the error message is Scanner cannot be resolved to a type. This message is telling you that the Scanner data type cannot be identified.
  3. Click the line titled Import ‘Scanner’ (java.util). The following statement is added to the beginning of the code file.

    The Java import statement works the same way as the Visual Basic Imports statement. The Scanner class belongs to the java.util package. So that you can reference the class and its members without having to use the class name, you use the import statement to import the class named java.util.Scanner.
  4. Run the program again. Enter your name in the Console View. The input is read, stored in the variable, and written back to the Console View as shown in the following figure:

Introduction to Eclipse Debugging