Once the NetBeans project is created (blog entry here if you missed it), we can get coding. Rather than abstractly going over the NetBeans UI, I’m just going to dive into getting the Hello World application running.

First off, go ahead and close the Start Page tab. It’s just in the way right now.

Creating a Class

You can ask NetBeans to create a class either through the File -> New File option or by right clicking on the Source Packages listing in the Projects tab to the left (it should be under a tree root called “HelloWorld”, which is the Project Name specified back at project creation time) and selecting “Java Class”. I prefer the latter option.

This brings up the new class creation screen which looks more daunting than it actually is. There are two important fields here. The Class Name field is exactly what it sounds like: the name of the class being created (don’t forget proper capitalization). The Package field indicates the package in which the class will be put, creating the package if it doesn’t exist (if you don’t know what a package is, leave this blank for now, soon enough this will be covered in class).

NetBeans will generate and display the exact path to the file being created. Notice that the file is put in the src directory (unless you accidentally right clicked on Test Packages initially). Also notice that if a package is specified, NetBeans will create the appropriate directory structure below the src directory.

Bitter Old Coder Comment: Just because NetBeans will put the class in the correct directory based on the package, don’t get lazy and fail to understand what that structure should look like. If the sheer knowledge isn’t motivation enough, realize it will be a question on one of my exams. :)

NetBeans - Create a Class

In the above screenshot, a new class named MyHelloWorldApp will be created in the default package. Once the Finish button is clicked, the new class will be opened in an editor tab. NetBeans will take care of some of the basic structure of the class, including class declaration and some basic javadoc comments.

Implementation

This section could be huge if I were to go into the various code writing assistance tools NetBeans offers. I’ll cover those more in future entries. For now, we’ll stick to the basics.

Navigate the left pane under Source Packages to find your class (trust me, this will look more interesting once packages are involved). If the class isn’t open, double-click the file to open it in an editor tab. I’ve provided a simple main method below:

Tip: One of the features of NetBeans is… well, to be honest, I don’t actually know the name. In IntelliJ, it’s referred to as a Live Template. The premise is that you type a few letters and press [tab] to have NetBeans expand the small abbreviations into full code. For example, in the MyHelloWorldApp class, type “psvm” (without the quotation marks) and press [tab]. It should automatically fill in the standard signature of the main method. If you’re having trouble remembering the abbreviation, it’s the first letter of each element in the signature: public static void main. While I’m on the subject, “sop” is another template that I’ll let you discover on your own.
public static void main(String[] args) {
    System.out.println("Hello World");
}

Compiling

There are a number of ways to compile depending on how much of the project you want to build. For now, I’ll stick with the left-pane-centric approach and suggest to right click the tree root (HelloWorld) and select Build.

So what just happened? Like last time, let’s look at a file browser to see what NetBeans actually did.

NetBeans - Browser After Build

In addition to the three directories created when the project was first created, there are two more now.

  • build – A build directory is used as a temporary directory into which project pieces are, well, built. Since we’re just dealing with a simple application here, the only task in building is to compile the classes, placing the resulting compiled classes in the classes directory. I say “temporary” in the sense that you should be able to delete it and recreate its contents by building the project.
  • dist – The dist directory represents a fully built application ready to be distributed. For now, this will likely just be a JAR file containing all of the classes in the project (I won’t get into the details of what a JAR is yet). This is also where NetBeans will place the generated javadoc HTML files if they are created (the Run -> Generate Javadoc command if you’re curious).

One thing I really like about NetBeans is that the project structure is consistent with many Java projects. Once you get accustomed to the ideas of what belongs in a src or build directory, it becomes pretty simple to navigate virtually any other Java project (for instance, the many open source projects available).

Also notice the source file for the class, MyHelloWorldApp.java, has been created under the src directory. Again, if this class were in a package, the package directory structure would be present under the src directory.

Running

Since the MyHelloWorldApp class contains a main method, it can be run through the java command or, more to the point, through NetBeans. Again, there are a number of ways to do this and again, I’ll stick with the approach that focuses around the Projects tab in the left-hand navigation. Right clicking on any file that contains a main method will have an option in the menu for Run File. Selecting that option will run the main method of that class.

The section beneath the editor tabs contains any output from the execution. In this case, it’s simply the output of the main method’s println statement.

run:
Hello World
BUILD SUCCESSFUL (total time: 0 seconds)

For now, don’t worry about what the “BUILD SUCCESSFUL” message means. While this showcases one of what I believe to be cooler aspects of NetBeans, it’s too much to explain right now. Suffice it to say, the word “successful” is good while “failed” isn’t.

Happy Coding

At this point, we’ve covered the minimum necessary to start coding in NetBeans. There are still a ton of useful features that I’ll cover in future posts, but this should be enough to get to a point where you can start to experiment and play around.

Comments are closed.