How to Use NetBeans 8.0 in CSCI 1226/8 Labs


Instrauctions are slightly different for later versions of NetBeans on your own computer. Most importantly, the Java Application project type can be found under the Java with Ant category.

Instructions to download and install NetBeans 11 (and JavaFX 13) can be found here.


Table of Contents

NetBeans Overview

We will be using NetBeans 8.0 as our IDE (Integrated Development Environment) in CSCI 1226/8. You must start it from the NetBeans 8.0 IDE icon on the lab computer desktop. (There is another NetBeans program in the application folder, but it is older and has some problems.)

I will generally have mine set to show three panes:

It might be helpful if you get yours set up the same way.

When we run a program, a fourth pane will appear below the source code pane on the right hand side of the window. You can leave that open even when your program isn't running.

The Projects pane looks like a directory structure -- and it pretty much is. The "top level" for each project shows a little cup of coffee icon. Inside each are two (or more) folders, of which Source Packages is the only one we need to worry about for now. Inside Source Packages is another folder, named after the package your code is in. (If your code is not in a package, then this folder will be called <default package>.) That's where your program files need to be.

Each project also corresponds to a folder on your account/USB drive. The folder on your drive has the same name as the project, and has a folder named src inside of it, and inside that is the folder for the package. (The files in <default package> appear directly inside the src folder.) When you're submitting files, it's the copy inside the package folder that you need to submit.

We will create a new project for each lab and each assignment. You might also want to create a separate project for programs from the notes. NetBeans expects that each project will have only one program in it, but we will sometimes put multiple small programs into one project to make them easier to work with. (In CSCI 1226/8, there will normally be only one or two files in each program anyway.)

Double-clicking on a file name in the Project pane will open that file in the source window. The source window has one tab for each open file. You can move quickly from one file to another using the tabs.

The Navigator pane is for moving between methods in a file. For now our programs will have only one method in them anyway -- main. When we start adding other methods, you can click the method name in this list to move directly to the definition of that method.

Back to top

Creating a New Project

  1. Select File > New Project... or click on the New Project... icon.
  2. Select Java under Categories, and Java Application under Projects. Click Next
  3. Fill in the name of your project. You can use the name of the lab or assignment itself (L00, L01, ..., A01, A02, ...) or you can give it another name entirely (but choose something that will remind you of what the program was for).
  4. Set the current location to your USB drive (preferably) or your J-drive, or the hard drive of the computer.
    If you choose the computer's hard drive, be sure to copy the project folder from the hard drive back to your USB- or J-drive before you leave. Otherwise it will be deleted overnight.
    Use the Browse... button to change the location if you need to.
  5. Make sure the Use Dedicated Folder for Storing Libraries boxes is cleared.
  6. The Create Main Class should be set. The name of the Main class should be something.SomethingElse. The bit before the dot should match the package name of the code you're downloading. (If you're creating a new program, then it can be whatever you like.) The bit after the dot should be the name of the program you're creating or downloading. Make sure it's exactly right.
  7. Click Finish.
Having a separate workspace for each lab will let us re-use program names. For example, we can have a HappyFace program in Lab01, and another HappyFace program in Lab02 -- and so we can keep our old code around to study from later.

Back to top

Downloading Code for a Lab

When a lab (or assignment) asks you to download code, the file will have a name and a package name that you should not change (for example, lab00.JavaAverage.java). In order to work on this code, you will need a project to put this code into. You should already have NetBeans started up, and the current lab's project created.

Download the code straight into the package folder in your lab project folder. When you return to NetBeans 8.0 it should be part of your project. If not use the Scan for External Changes command at the bottom of the Sources menu.

An alternative way to add code to a project is to drag the file from the folder it's currently in onto the package for the project you want to add it to. This works for code you've downloaded from our web site, and also for code you've created using another program (such as Notepad).

Note, however, that you will then have two copies of that file. The original that you dragged will still exist, and a new copy of the file will be included in the project folder. If you edit the file using NetBeans, it'll be the copy in the project folder you're editing, not the original. Make sure you don't pass in the original (unmodified) copy.

Back to top

Finding Syntax Errors

NetBeans shows you your mistakes as you type. It will draw a red, squiggly line under the bad code to show you where it thinks the problem is. It will also put a small red exclamation-point icon over the line number for that line.

There may also or instead be a light-bulb icon. The light-bulb is for a suggestion NetBeans has. If there's both an error (red exclamation point) and a suggestion (light-bulb) the two are combined into one icon.
If you hover over the small icon a message will appear saying what NetBeans thinks the problem is. It's usually right. (Not always, but usually.) You should read the message whenever you get an error so that you come to recognize the mistakes that cause that message. For example, a common mistake to make is to spell a word wrong. The message NetBeans shows you will start with the words cannot find symbol, which is the Java compiler's way of telling you it doesn't recognize that word. When you see that message, you probably spelled a word wrong.

If there are mistakes like this in your program, you won't be able to run it. If you try to run it, it either won't work at all, or NetBeans will pop up a small window saying One or more projects were compiled with errors and asking you if you want to Run Anyway. Don't run it anyway. Click the Cancel button instead.

If you run it anyway, it'll use the old version of your program -- the last one that compiled. It will ignore all the changes you made since then, so it'll still have the same problems that made you edit it again.

Back to top

Running a Program

After you have written a program, you will want to run it. If you have only one program in your project, you can use the Run Project... command or icon. The command is found in the Run menu. The icon is the green triangle pointing right on the toolbar just below the menu bar.

If this is the first time you've run your project, NetBeans may tell you that the project has no main class, and ask you to Select the main class. It will offer you a list of all the files that have a main method.

If there's nothing in the list, then you either don't have a program, or you haven't saved your program file yet. Save the program file and try again. If there's still nothing in the list, then you didn't declare your main method properly. See what's wrong and fix it.)
Select your program file from the list and click OK.

If there are multiple programs in your project, only one of them can be the project main class. The other ones will have to be run separately. Open the file you want to run, and select its tab in the source code pane. Choose the Run File... command from the Run menu. Alternatively, you can right-click (for Windows) the file in the Project pane and choose the Run File... command from the pop-up menu that appears.

In either case, the output will appear in the Output pane below the source code pane. (It will become visible if it was previously hidden.) If the program needs input it will pause to let you type. Your insertion pointer (or "focus") needs to be in the Output window in order for the input to go to the program. You may need to click inside the output window in order to get the insertion pointer there.

When the program finishes, NetBeans prints a green message to report that the "build" was successful.

Sometimes your program may crash, in which case NetBeans prints a message in red saying what the problem was. For example, when you type the wrong kind of input (a word where the program was expecting a number, for example), the message starts with the line Exception in thread "main" java.util.InputMismatchException. At the end of this message is a little hyperlink that will take you to the line the program was working on when the "exception" happened.

An exception usually means that the user did something wrong, and there's not much the programmer can do about that. We won't learn how to deal with exceptions until later in CSCI 1228. However, if your program keeps crashing even when the input looks right, it's probably something the programmer did wrong. This time you'll need to figure out the problem without any help from NetBeans beyond what kind of an exception it was and where it occurred.

If the output window is alreay open, you can click the small, green double-right-arrows at the top left corner of that window to run the same project or file again.

Back to top

Creating a Program

Sometimes (especially for assignments) we will ask you to create a program and we won't give you any code to start from. In this case you need to create your own program.

If it's an assignment, you should start by creating a new project for the program. If it's a lab, it'll be fine to put it in the same project with any code you downloaded. (That means all the files you need to submit for the lab will be in the same folder, making it easier to submit them.)

Make sure that you're in the project you want the file in. Choose the New File... command from the File menu. Make sure the project is the one you want to create the file in. Select Java under Categories and Java Main Class under File Types. Click Next.

Fill in the name of the class -- which should be the same as the name of the file we asked you to create, but without the .java part. Make sure the Package box has the name of the package you're using. Click Finish.

A window should open with some starter code in it. The first comment (lines 1 to 5) can be deleted entirely. The second comment (now lines 2 to 6 (or 1 to 5)) is your Javadoc comment, and it's where you need to fill in your name and student number (next to the @author line). Leave the comment on lines 8 to 10 (or 7 to 9) in, but don't worry about it for now.

Start your program by replacing the comment on line 12 (or 11) with pseudocode comments for your program. Then edit the code as required.

Back to top

Returning to Your Work

When you return to the lab after a break, you may want to go back and work on a project you were working on before. It's possible that your previous work will be listed on the start page, under the Recent Projects heading, or that it will already be in the Projects pane (or both). If so, you can simply click it and return to what you were working on.

If your work is not immediately, don't worry. Choose the Open Project... command from the File menu. Navigate to where you keep your projects. Any NetBeans projects you have saved will appear in the Open Project dialog with little cup-of-coffee icons next to them. Double click the one you want, and it'll open.

Back to top