How to Use NetBeans 8 in CSCI 1226 Labs


Table of Contents

Installing NetBeans

Here are some videos I made showing me installing NetBeans 8:

Here are the corresponding videos for NetBeans 12 (later versions of NetBeans should be very similar):

And here is a link for those who can't (or don't want to) use NetBeans:

Back to top

NetBeans Overview

We will be using NetBeans as our IDE (Integrated Development Environment) in CSCI 1226. There should be a NetBeans 8.1 IDE icon on the lab computer desktop. (It's a blue cube icon -- NetBeans Icon.) If not, you should be able to find it in the Applications/Programming folder.

I will generally have mine set to show three panes:

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

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, which may be called <default package>, but will normally have a different name (one that's similar to the project name, like Monopoly/monopoly in the example above). That's called a package and it's where your program files need to be.

Each project also corresponds to a folder on your computer. For example, the project above is saved on my home computer, and the files are found in my NetBeans Projects/Monopoly/src/monopoly folder.

NetBeans Folders
In the lab the computer will want to save them on your student account (the J drive, which has your s-number as its name), but it would also be in the NetBeans Projects folder. You can change where you put the project folder (for example you might want to put it on a thumb drive so you can work on it at home and in the lab), but your programs will be in the project/package folders (Monopoly/src/monopoly, in this example).
When you're submitting files, you need to browse to the correct project/package folder to find the files you've been working on.

We will create a new project for each lab and each assignment. You might also want to create a separate project for testing 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, 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 Program

When we ask you to create a new project, we will give you a project name and a program name. For example, we might ask you to create a program named AboutMe in a project named Asgn1. You should use those exact names when you create your project; otherwise your program won't work properly when the markers get it!

  1. Select File > New Project... or click on the New Project... icon.
    New Project Command
  2. Select Java under Categories, and Java Application under Projects.
    Java Application
    Click Next.
    Those instructions should work for versions 9 and 10 as well.

    In version 11 you should choose Java with Ant from the Categorieslist (but don't select any of the items inside the Java with Ant folder). You can then select Java Application under Projects as usual.

  3. Type the name of the project in the Project Name box at the top. The other boxes will fill in automatically.
    You may want to change the Project Location, as described above. Use the Browse... button to navigate to your preferred location.

    NOTE: If you choose a lab 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.

    The Use Dedicated Folder for Storing Libraries check-box should be clear. The Create Main Class check-box will be selected, but we'll want to change the name in the box: put the name of the program after the dot. (In our example, we change asgn1.Asgn1 to asgn1.AboutMe.)
    Name and Location
    Click Finish.
NetBeans will create a new project with a new program in it.
NetBeans Folders
(The code in your program will look a little bit different. See setting options to see how to change your program to be more like mine.)

Back to top

Downloading Code for a Lab

Sometimes (especially for a lab) we will give you a program that you're supposed to make changes to. You should still create a program according to the usual instructions. Use the project name and program name we provide. The difference is that you're going to replace the code in your new program with the code we've given you.

One way to replace the new program with the code we've given you is to just download the file we give you into the package folder. When your browser asks you if you want to replace the existing file, say yes. This method works for adding a second program to a project, too. Just download the program into the package folder; it should show up in the package folder in NetBeans, too.

A different way to replace NetBeans' starter code with given code is to open the given code in the browser, copy it, then paste it over NetBeans' starter code.

Back to top

Changing How New Programs Look

To get a quicker start on your new programs, choose the Templates command from the Tools menu. In the Java folder, choose Java Main Class, then click the Open in Editor button. The window will have something like the following in it:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "${project.licensePath}">

<#if package?? && package != "">
package ${package};

</#if>
/**
 *
 * @author ${user}
 */
public class ${name} {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }

}
Change it to what you want it to be. For example, I've changed mine to be
<#if package?? && package != "">
package ${package};

</#if>
import java.util.Scanner;

/**
 *
 * @author Mark Young (A00000000)
 */
public class ${name} {

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

}
That gets rid of the "license" at the beginning, adds a command to import Scanner, and replaces my user name (my s-number) with my actual name and A-number. (I also deleted the "javadoc" comment in front of main. We won't be using it.)

You can do similar things with the other kinds of Java code files. We'll be using Java Class later in the term.

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 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, not the programmer. 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.

We won't be learning how to deal with exceptions, but the textbook explains how in chapter 9, if you're interested.

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

Formatting Your Code

It's hard to read code that's messy, so we want to keep it neat. NetBeans does a pretty good job of helping you keep it neat (by automatically indenting your code, for example), but it's possible to get it pretty messed up when you're moving code around.

Fortunately there is a very easy way to fix the formatting on your code. Right-click (or equivalent in other operating systems) on the background of your program. Select the Format command from the pop-up menu. Voilà!

Back to top