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:
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.
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:
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.
Show your lab instructor the new project you have created.
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
Show your lab instructor that you have mounted the correct file system.
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.
System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one.");
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.
Compile and run your java program
Show your instructor the result of your running program. Try to match the following display before showing off your work.
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.
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/ inflated: META-INF/MANIFEST.MF created: Fixit/ inflated: Fixit/FixIt.java
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
To see the contents of the java file that you just downloaded, you need to open it in NetBeans:
[user@mach dir] netbeans &
In order to fix the errors in your program, we need to be clear on a few facts regarding java programming:
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.
Show your instructor that your program compiles and runs.
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.
Follow the instructions below to set up your project and include the Keyboard class.
[user@mach dir] netbeans &
// 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");
Show your instructor your program and explain how the output was created.