Java For Students
usenetbeans.htm
Using Netbeans 4.1 - a brief introduction
When using NetBeans, you must create a 'project'. This is a folder containing Java code, plus
associated information.
Even if you are writing a short single-file program, a project must be created.
In the following, let us assume that we are writing a program named Demo.java. Additionally,
we assume that you have created a folder named JavaPrograms, which will hold a collection of projects.
The name of the project folder can be anything, but for consistency we will name it DemoProject in this example.
Here are the steps:
-
Run NetBeans from the desktop icon, or the start menu.
-
Choose File | New Project
-
Under Categories, select General. Under Projects, select
Java Application and click Next.
-
Under Project Name, enter DemoProject.
By Project Location, click Browse, and locate your JavaPrograms folder.
Ensure that
Set as main:
Create main class:
are both checked.
Click finish
-
After a few seconds, the main NetBeans screen will re-appear.
Look at the top left area.
You will see two panes, titled Files and Runtime. (If Files is
not visible, use the Window menu : Window | Files )
Ensure Files is selected.
Double-click on DemoProject to open it up. You will see:
DemoProject
build
nbproject
src
test
...etc
Right-click on src, and select New... then Empty Java File.
A new window pops up. For the Class Name, enter Demo. (Note the capital D, and do not enter '.java')
click Finish.
-
Type your program into the Demo.java region, to the right of the screen.
-
To compile the program, move to the Files pane, locate the src folder, and Demo.java inside it.
Right-click on Demo.java, and select Compile File (or press F9).
Any compilation errors will be shown in the bottom pane, as blue links. Click
on an error to be positioned at the line in question.
-
When there are no errors, run by going to the src folder, right-click
on Demo.java, choose Run File(or press Shift and F6)
-
When editing, it is useful to have your code neatly indented. Right-click on your code, and
select Reformat Code.
-
At the end of your session, use File | Save All
-
When you run NetBeans again it is much simpler: use File | Open Project to re-load your work.
Debugging
It is possible to run the code a line at a time, and inspect the values of variables.
-
Put a 'breakpoint' on a line (typically near the top of a method) by clicking to the
right of the line. It is highlighted in pink.
-
To run, move to the left of the screen, ensuring the Files panel (not the Runtime one) is selected.
If the project is not expanded, double-click to open it, and open up the src folder.
Right-click on Demo.java, choosing Debug File.
-
As the program runs, it pauses when it reaches your breakpoint. Press F8 (Step over) to step to the next instruction. Note
that if the instruction is a method call, it will be executed at fiull speed. If you want to step through
the called method, use F7 (step into)
Look at the lower right of the screen, and open up the Local Variables area. The current
values are shown. Note that the green highlighted line is about to be executed, so values
it is about to change have not actually been changed yet.
The debugger is powerful, and has many more facilities!
Mike Parr
1/June/05