Inroduction to Netbeans and Java
This lab covers the following topics:
• Install NetBeans
• Getting Started with NetBeans
• Creating a Project
• Creating a Class
• Compiling a Class
• Running a Java Application
• Forcing a Program to Terminate
This tutorial applies to NetBeans 6, 7, or a higher version.
This tutorial is for students who are currently taking a Java course using NetBeans with Introduction to Java Programming. You can use the JDK command line utility to write Java programs.
JDK command line utility
The JDK command line utility consists of a set of separate programs, such as compiler and interpreter, each of which is invoked from a command line. Besides the JDK command line utility, there are more than a dozen Java development tools on the market today, including NetBeans, JBuilder, and Eclipse. These tools support an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface. Using these tools effectively will greatly increase your programming productivity.
This brief tutorial will help you to become familiar with NetBeans. Specifically, you will learn how to create projects, create programs, compile, run, and debug programs.
Activity 1:Install NetBeans
Download and install JDK 1.8 and NetBeans 8.0 from one bundle at
Activity 2:Getting Started with NetBeans
Assume that you have successfully installed NetBeans on your machine. Start NetBeans from Windows, Linux, Mac OS X, or Solaris. The NetBeans main window appears, as shown in Figure 1.
The NetBeans main window is the command center for the IDE.
The NetBeans main window contains menus, toolbars, project pane, files pane, runtime pane, navigator pane, and other panes.
1.1 The Main Menu
The main menu is similar to that of other Windows applications and provides most of the commands you need to use NetBeans, including those for creating, editing, compiling, running, and debugging programs. The menu items are enabled and disabled in response to the current context.
1.2 The Toolbar
The toolbar provides buttons for several frequently used commands on the menu bar. The toolbars are enabled and disabled in response to the current context. Clicking a toolbar is faster than using the menu bar. For many commands, you also can use function keys or keyboard shortcuts.
For example, you can save a file in three ways:
• Select File, Save from the menu bar.
• Click the “save” toolbar button ( ).
• Use the keyboard shortcut Ctrl+S.
TIP: You can display a label known as ToolTip for a toolbar button by pointing the mouse to the button without clicking.
A workspace is a collection of windows that are pertinent to performing certain types of operations, such as editing, execution, output, or debugging. The workspace windows can be displayed from the Window menu.
Activity 3:Creating a Project
A project contains information about programs and their dependent files, and it also stores and maintains the properties of the IDE. To create and run a program, you have to first create a project.
Here are the steps to create a demo project:
1. Choose File, New Project to display the New Project dialog box, as shown in Figure 2.
The New Project dialog box enables you to specify a project type.
2. Select General in the Categories section and Java Application in the Projects section and click Next to display the New Java Application dialog box, as shown in Figure 3.
The New Java Application prompts you to enter a project name, location, and a main class name.
3. Type demo in the Project Name field and c:\michael in Project Location field.
4. (Optional) You can create classes after a project is created. Optionally you may also create the first class when creating a new project. To do so, check the Create Main Class box and type a class name, say First, as the Main Class name.
5. Click Finish to create the project. The new project is displayed, as shown in Figure 4.
A new demo project is created.
Activity 4:Creating a Class
You can create any number of classes in a project. Here are the steps to create Welcome.java.
1. Right-click the top demo node in the project pane to display a context menu, as shown in Figure 5.
You can create a new class in a project
Choose New, Java Class to display the New Java Class dialog box, as shown in Figure 6.
The New Java Class dialog box enables you to specify a class name, location, and package name to create a new class.
2. Type Welcome in the Class Name field and select the Source Packages in the Location field. Leave the Package field blank. This will create a class in the default package. (Note that it is not recommended to use the default package, but it is fine to use the default package to match the code in the book. Using default package is appropriate for new Java students. Section 10, “Using Packages,” will introduce how to create a class in a non-default package.)
3. Click Finish to create the Welcome class, as shown in Figure 7.
A new Java class is created in the project.
The source code file Welcome.java is placed under the <default package> node, because you did not specify a package name for the class in Figure 6.
4. Modify the code in the Welcome class to match Listing 1.1, as shown in Figure 8.
The source code for Welcome.java is entered.
You can show line numbers in the Source Editor by choosing View, Show Line Numbers from the main menu.
The source file Welcome.java is stored in c:\michael\demo\src.
Activity 5:Compiling a Class
To compile Welcome.java, right-click Welcome.java to display a context menu and choose Compile File, or simply press F9, as shown in Figure 9.
The Compile File command in the context menu compiles a source file.
The compilation status is displayed in the Output pane, as shown in Figure 10. If there are no syntax errors, the compiler generates a file named Welcome.class, which is stored in c:\michael\demo\build\classes.
The compilation status is shown in the output pane.
NOTE: When you compile the file, it will be automatically saved.
Activity 6:Running a Java Application
To run Welcome.java, right-click Welcome.java to display a context menu and choose Run File, or simply press Shift + F6, as shown in Figure 12.
The Run File command in the context menu runs a Java program.
The output is displayed in the Output pane, as shown in Figure 13.
The execution result is shown in the Output pane.
NOTE: The Run File command invokes the Compile File command if the program is not compiled or was modified after the last compilation.
Activity 7:Forcing a Program to Terminate
If a program does not terminate due to a logic error, you can force it to terminate by clicking the Stop icon, as shown in Figure 18.
You can force a program to terminate from the runtime pane.
1. Every student is required to make installation on his / her personal computer before next lab