NetBeans Introduction

This week's lab will introduce you to the NetBeans IDE. An IDE, or Integrated Development Environment, is a powerful programming tool. It provide a graphical interface for managing program source files, compiling and debugging code, and generating installation packages.

The NetBeans IDE is written totally in Java. Consequently, it can run on any computer that supports Java. NetBeans can be used to create programs in a number of different languages, but in CSCI 201 we use it only for Java. In CSCI 202, a few of the more complex features of NetBeans will be introduced, but even professional programmers only use a smart part of NetBeans capabilities.

The goals of this lab are to:

  1. Create a NetBeans project
  2. Fix a Java program with lots of syntatic errors
  3. Create and run a Java program in NetBeans
Getting NetBeans

If you are sitting in a Computer Science Lab room right now, then NetBeans should already be installed on your computer. If you need to install NetBeans on your home computer, you should first download a recent version of the Java Software Development Kits from Sun's Java site. Be sure to download the SDK, Software Development Kit, and not merely the JRE, Java Runtime Environment.

Once the Java SDK is installed, you can download the NetBeans IDE. Although Sun provides a "co-bundle" of both the Java SDK and NetBeans IDE, we suggest you install these packages separately. This will ensure that your favorite web browser will be customized with the latest Java plug-ins.

Starting Netbeans

Windows users can click through the long, and rather repetitive, All Programs >> NetBeans IDE >> NetBeans IDE 3.5 >> NetBeans IDE start menu sequence of selections to run NetBeans.

Linux users can start up a Linux terminal session by clicking through the start menu sequence System Tools >> Terminal or by right-clicking in an unused part of the desktop and then requesting a new terminal. Then type the command netbeans &. The "&" runs the command in the background and leaves the Linux terminal window free to accept additional commands.

[user@mach dir] netbeans &

If all goes well, you should see a splash screen and then a screen that looks something like this:

Creating a New Project
To Get Started ...
Like many IDEs, NetBeans uses projects to organize your work. The project stores the locations of your Java program files and the instructions for building and running your compiled code. Usually you will create a project for each application you develop. You don't have to create a project to work on a Java program, but it's a really good idea to do so.

To open the Project Manager window, click Projects >> Project Manager on the menu bar. You'll see a screen that looks like this:

To create a new project, click the New button and then enter the name of your new project. The Project Manager can also be used to open and delete previously created projects.

Information regarding your projects is contained in special XML files contained in your home directory. You don't want to look at these files, but we thought you'd feel more comfortable knowing your projects will still be around the next time you start NetBeans.

Your Assignment ...
Instructor Checkoff ...

Show your lab instructor the new project you have created.

Mounting Filesystems

Every operating system has its own special way of naming directories and files, and NetBeans must be able to provide a consistent interface in all of these environments. This is accomplished by mounting "filesystems" within a project. Whenever NetBeans wants to modify a file containing a Java program or execute a Java class, it finds that file within the mounted filesystems of a project. A filesystem is just a pointer to a place on your hard drive where you will store files. A note to you Linux gurus: NetBeans filesystems and Linux file systems are totally unrelated.

To mount a filesystem, click File >> Mount Filesystem on the menu bar. The following window will be displayed:

Now select the Local Directory option to choose the filesystem type. Next, click Next. Then navigate through your directories, or folders, by quickly double-clicking on entries. You can use the Up One Level button to move up the file hierarchy. Generally lab descriptions will tell you exactly which folder to select.

Sometimes you must create new folders. Do this by clicking on the Create Folder button . The new folder will be called New Folder and you will, of course, want to rename it to something less silly. The renaming maneuver can be difficult for even the most experienced Windows or Linux user. It requires that you slowly double-click on the words "New Folder" until they are highlighted. Then you can edit the file name. We recommend that you could to three between each click.

Suppose the lab tells you to mount the folder csci/201/Lincoln, and the 201 folder exists but doesn't contain the Lincoln folder. To do this you must first navigate to the csci folder and then the 201. Inside the 201 folder create a new folder called New Folder, and then rename it to be Lincoln.

Once you have located the folder you want, click the finish button to add it to your project. The new filesystem will be shown in the Explorer Window

Your Assignment ...
Instructor Checkoff ...

Show your lab instructor that you have mounted the correct file system.

Creating a new Java program file
To Get Started ...

Java program files will contain the code you write in this course. Let's learn how to create one within NetBeans.

First, be sure the the file system in which you are creating a Java file is selected within the explorer, as it was in the previous picture. To begin the process of creating a new Java program file, either click the new button in the menu bar at the top of the NetBeans window or go through the File >> New menu choices. This will bring up a list of templates you can use to create a file.

Click on Java Classes which will then allow you to choose the Java Main Class template. Now, click Next.

At the point, you will be able to type in a name for your new file. Note that files are placed within filesystems and that filesystems may have several levels of folders. Make sure that you create a file within the appropriate filesystem and folder. Once you have specified your file, click Finish to create a skeleton Java program file. If you click Next, you have the option of adding all sorts of program elements, such as variables and methods.

Your Assignment ...
  1. In NetBeans, create a new file called inside your Lincoln folder. (Note that NetBeans automatically adds the ".java" extension, you simply specify the name Lincoln).
  2. Copy the following java code and paste it into the body of the main() method, right between the two curly braces.
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
  3. Save your file by clicking the Save button Netbeans Save Button in the top toolbar
Compiling and Running your Java program
To Get Started ...

Compiling is the process of transforming the Java code you write into ByteCode which can be run on your system.

To compile a file, open it in the Source Window and then click the Build button . If you look in the Explorer Window, a file which needs compiling will have little red binary number to the right of its icon .

To run a file, click the Execute button .

Only classes which contain a Main method or which extend the Applet classes may be executed. Both of these types of classes can be run by pressing the Execute button.

Your Assignment ...

Compile and run your java program

Instructor Checkoff ...

Show your instructor the result of your running program. Try to match the following display before showing off your work.

Downloading Jar Files

In this course, you will frequently be asked to download collections of files for use in labs and assignments. We're going to use jar, or Java archive, files for this purpose in CSCI 201. An archive file typically contains a collection of files. You must extract the files from an archive file before using them. You're already familiar with archive files if you've used zip files; jar files are similar to zip files.

Your Assignment ...

The archive file for this lab is named Fixit.jar. Depress the shift key and right-click on the link in the previous sentence, to Save Fixit.jar to your computer. Save the file in your csci/201 folder.

Give the following commands in your Linux session window to extract the compressed files from Fixit.jar:

[user@mach dir] cd csci/201
[user@mach dir] jar xfv Fixit.jar
  created: META-INF/
  created: Fixit/
  inflated: Fixit/

The first command, cd csci/201, settings our current working directory to be csci/201, the directory containing the archive file. The command jar xfv Fixit.jar unpacks or "expands" the archive file.

Give the command ls -R (dir in windows) to see the directory (and its contents) created by unpacking the archive file.

[user@mach dir] ls -R

Opening the File

To see the contents of the java file that you just downloaded, you need to open it in NetBeans:

  1. If NetBeans is not already running, give the command to start NetBeans in your Linux session window.
    [user@mach dir] netbeans &
  2. Create a new project for the FixIt program.
  3. Mount the FixIt directory. (In lab, always mount the directory containing the java file that you'll be working on.)
  4. Open the file in the NetBeans' editor by double-clicking on it with in the NetBeans' explorer window.
  5. Try to compile the file and notice that within the output in the Compiler output window two errors are reported.

Compiler Errors

In order to fix the errors in your program, we need to be clear on a few facts regarding java programming:

  1. All statements in java must end in a semicolon, ";".
  2. Brackets, such as "{}", are used to break a program into functional units; these units may be nested. Each opening bracket, "{", must have a matching closing bracket, "}".
  3. Java is a case sensitive language. The "object" named "System.out" used to display information to the terminal window can not be referred to as "SYSTEM.OUT" or "system.OUT", for example.

Now let's return to NetBeans. Notice that not only are there error messages in the Compiler output window, but there are also indications of problems in the Editing window. Notice the red patches to the right of 2 lines of code in the program.

By looking at both the error reports in the compiler window and the information in the editing window you can find out a lot about what's wrong with a program.

Double-click on the first error in the compiler output window and notice that the indicated line of code, line number 19, highlights in red. Now place your mouse over the red patch on the same line of code and wait until the pop-up message appears.

Using both the messages in the Editing window and the compiler error messages, try to correct the errors in the program. Always start with the first error listed in the compiler output window and recompile after every change that you make.

Instructor Checkoff ...

Show your instructor that your program compiles and runs.

First Program

Now that you've seen how everything works, let's put it all together to write a simple program. In this program, you will do three things: (1) read input from the keyboard, (2) store that information in memory, and (3) output information to the terminal window. The pseudo-code for this program would look something like:

  prompt user 
  read information
  store information
  write information

You've already seem how to write output to the terminal window using System.out's println() method. In this program, we'll also read input from the keyboard using a method that belongs to the Keyboard class. The Keyboard class was written by the authors of your text and is not part of the Java SDK so you must make the code available to the compiler as described below.

Your Assignment ...

Setting up the Project

Follow the instructions below to set up your project and include the Keyboard class.

  1. If NetBeans is not already running, give the command to start NetBeans in your Linux session window.
    [user@mach dir] netbeans &
  2. Create a new project called Hello.
  3. Create the directories csci/201/Hello and csci/201/Hello/cs1.
  4. Store the Keyboard class inside of the csci/201/Hello/cs1 folder. (Depress the shift key while right-clicking on the link to save the file to your computer.)
  5. Mount the directory csci/201/Hello directory.

Writting the Source Code

  1. In the csci/201/Hello directory, create a new java Main class called Hello.
  2. Edit the program template to indicate that you will be using the Keyboard class and that it is stored in the subdirectory cs1. The necessary line of code is
    import cs1.Keyboard;
    placed as shown below.

  3. Now add the following lines of code to the main() method:
      // prompt user
      System.out.println("Please enter your name");
      // read input and store it
      String name = Keyboard.readString();
      // write output
      System.out.println("Hello there " + name + " welcome to CSCI 201");
    as shown below:
  4. Compile and run your program.
Instructor Checkoff ...

Show your instructor your program and explain how the output was created.