How to Use NetBeans

This page is based on an original document by Mark Young.

Table of Contents

NetBeans Overview

As you may know, one of the many programs (editors and IDEs) that you can use for developing Java software is the NetBeans IDE (Integrated Development Environment). NetBeans appears to be the tool of choice for the introductory course, so you may continue to use it, and this document may serve as a reminder of some things, and/or as an introduction to some new features of NetBeans.

NetBeans is free so you can download it from the Oracle internet site and install it at home if you wish. On the campus desktop you will find the latest version available at the time of desktop installation. It should not matter if your version at home is not exactly the same as the one on campus.

When you start NetBeans you will generally see these panes:

When you run a program, a fourth pane will appear below the source code pane on the right hand side of the window to show you progress and output. This pane will show up every time you run the program, so you might as well leave it 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 you need to worry about most of the time. Inside Source Packages is a folder called <default package>. That's where your source code program files need to be.

Each project also corresponds to a folder somewhere on your account, on the computer where you're logged in, or perhaps on a USB drive plugged in to that computer. That folder will have the same name as a project, and will have a folder named src inside of it. The files in <default package> also appear inside this src folder. When you're submitting a source code file, it's the copy inside the src folder where you've been working that you need to browse to, and any corresponding class files should be found in the build\classes folder.

You can opt to create a new project for each lab exercise and/or each submission, but that is not necessary and will chew up more "real estate" than you might like. An alternate approach is to have a single project (called Test, say) and move files into and out of that project as you work on them or finish working on them. As a general rule, in this scenario it's better to "move" files than to "copy" them, since multiple copies of the same file can lead to confusion (and submission of the wrong file(s) for an assignment).

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. In a simple program there may only be one method -- main(). When a program has several 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..., click on the New Project... icon in the toolbar, or press Ctrl+Shift+N.
  2. Select Java under Categories, and Java Application under Projects. Then click Next.
  3. Fill in the name of your project and be sure to choose something that will remind you of what the project was about.
  4. Set the current location appropriately, which might be your USB-drive if you're using one and want to take your work with you, your J-drive if you're on campus and want to leave your work here, or the hard drive of the computer you're working on (probably D: if you're on a campus computer) if using the other options is slowing you down as you work on your program.
    If you choose the computer's hard drive, be sure to copy the project folder from the hard drive back to your USB-drive or J-drive before you leave. Otherwise it will be deleted overnight.
  5. Use the Browse... button to change the location if you need to. Make sure the Use Dedicated Folder for Storing Libraries and Create Main Class boxes are cleared and then click Finish.
Although, as we mentioned above, having a separate workspace for each development exercise takes up more space, one advantage of doing things this way is that you can easily re-use program names. For example, we can have a MyGreatProgram.java program in Submission01, and another MyGreatProgram.java program in Submission02.

Back to top

Creating a Program (Source Code) File from Scratch

Sometimes you will need or want to create a program "from scratch" and don't have any code to start from. In this case you need to create your own program. Other times you may have one or more files supplied (or carried over from previous work) and you have to extend or modify those files and perhaps create others to work with them. Depending on the situation, and your general practice, this is where you have to decide whether to create a new project or to just use an "all-purpose" project that you have previously set up.

If you're creating a new program or project, first make sure that you're in the project folder you want the file in. Choose the New File... command from the File menu. Select Java under Categories and Java Main Class under File Types. Click Next.

Fill in the name of the class -- which may have to be the same as the name of the file you have been asked to create, but without the .java part. Leave the Package box empty.

There will be a little warning icon at the bottom of the window saying that it's "highly recommended that you do not place Java classes in the default package". Don't worry about it (at least for the moment). Unless you are given explicit instructions to the contrary, do not put any of your source code files into a package (none of them should therefore contain a package statement).
Click Finish.

A window should open with some starter code in it, most of which you can remove. In fact, leave only what is necessary to get started, according to the identification and style guidelines you are required to follow.

Now away you go ... begin editing the code as required.

Back to top

Starting with an Existing Program

Sometimes you will want to start with an instructor-supplied "shell" or sample program. In this case, simply move the starting file into the src folder of your project, carry on as above, and then copy the finished source code file back to its original location.

Back to top

Running a Program

After you have written a program, you will want to run it. If you have only one source code file 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 and you have more than one source code file in the 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.

In Java a project (or program) can be comprised of multiple source code files, each of which contains a main() method. However, a project can have only one project main class (the one where the program starts when it begins to execute). The other ones will interact with the main class and themselves as the project runs. Of course, if each class has a main() method (a good thing, actually) each class can be run separately, which allows that class to be tested independently of any other classes. Open whatever file you want to run, and select its tab in the source code pane. Then 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. (This is when that pane 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. 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 already 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

Making Sure Your Program Runs Outside NetBeans: The "Acid Test"

Copy your program file(s) to some "arbitrary" folder away from the folder you were using to develop your program with NetBeans. Open a command window on this new folder and try to run your program from the command line in this new location. If it does not run there, then you have made some kind of error during your development. Perhaps you included a package statement and placed your program into a package, for example. Correct whatever is wrong and perform this test again. Make sure your program passes this "acid test" before submitting it.

Finding Syntax Errors

NetBeans shows you your mistakes as you type by drawing 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 (a function or method name, for example).

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 do 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

Creating a jar File for Distribution

When you want to package your code to send it to someone else so they can run it, you will want to create a jar file. From the Run menu select the Clean and Build Project command (or press <Shift+F11>). NetBeans will remove all old code and compile everything from scratch. It will also create a dist folder next to the src folder in your project folder. Your jar file (the file with the .jar extension) is in the dist folder.

If your project is a GUI, then others should be able to run your code by double-clicking the jar file. They may need to change some settings on their computer to convince it that this is OK. Google for the details if you need them.

If your code uses the console for input and output, others will need to start it up inside a console window. It's possible to set up shortcuts that open a console window and then run the jar file in that window. How to do so depends on your operating system. Google for the details if you need them. We will also discuss this option in the lab.

Back to top

Adding a jar File to Your Project

Sometimes code you need or want to use is contained in a "jar file" (a file with a .jar extension), and you can import such a jar file into your own project. To add a jar file to your project, you must first download it to some convenient location. (It does not have to be in your project folder.)

Next, in your project, right-click on the Libraries folder (it's right below your Source Packages folder in the Projects pane). Select the Add JAR/Folder... command. Navigate to where you saved the jar file, and double-click it (or select it and click the OK button).

To use the classes from the jar file, you can now just import them in your code files.

Back to top

Returning to Your Work after a Break

When you return to work in NetBeans 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 on it and return to what you were working on.

If your work is not immediately available in an obvious way, 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 will open.

Back to top

Additional Advice, Hints and Suggestions

  1. Be sure to download and print a copy of the handy NetBeans Keyboard Shortcuts document, which is available under the Editor | IDE Reference link on the course website, or from the NetBeans Help menu.
  2. Be sure to import into your NetBeans the zip file of settings mentioned by your instructor so that your source code may be easily formatted in the way required by the course specifications for Java source code.

Back to top