2. Writing a Hello-world Java Program in NetBeans

Step 0: Launch NetBeans

Launch NetBeans. If the "Start Page" appears, close it by clicking the "cross" button next to the "Start Page" title.

Step 1: Create a New Project

For each Java application, you need to create a "project" to keep all the source files, classes and relevant resources.

  1. From "File" menu ⇒ Choose "New Project...".
  2. The "Choose Project" diglog pops up ⇒ Under "Categories", choose "Java" ⇒ Under "Projects", choose "Java Application" ⇒ "Next".
  3. The "Name and Location" dialog pops up ⇒ Under "Project Name", enter "FirstProject" ⇒ In "Project Location", select a suitable directory to save your works ⇒ Uncheck "Use Dedicated Folder for Storing Libraries" ⇒ Uncheck "Create Main class"  ⇒ Finish.

Step 2: Write a Hello-world Java Program

  1. Right-click on "FirstProject" ⇒ New ⇒ Java Class (OR choose the "File" menu ⇒ "New File..." ⇒ Categories: "Java", File Types: "Java Class" ⇒ "Next").
  2. The "Name and Location" dialog pops up ⇒ In "Class Name", enter "Hello" ⇒ Delete the content in "Package" if it is not empty ⇒ "Finish".
  3. The source file "Hello.java" appears in the editor panel. Enter the following codes:
    public class Hello {
        public static void main(String[] args) {
            System.out.println("Hello, world");
        }
    }

Step 3: Compile & Execute

There is no need to "compile" the source code in NetBeans explicitly, as NetBeans performs the so-called incremental compilation  (i.e., the source statement is compiled as and when it is entered).

To run the program, right-click anywhere in the source (or from the "Run" menu) ⇒ Run File. Observe the output on the output console.

Notes:

  • You should create a NEW Java project for EACH of your Java application.
  • Nonetheless, NetBeans allows you to keep more than one programs in a project, which is handy for writing toy programs (such as your tutorial exercises). To run a particular program, open and right-click on the source file ⇒ Run File.