Hello World

© 2012 by W. Patrick Hooper. Licensed under a Creative Commons Attribution 3.0 Unported License.

Purpose: Create a first program in Java and a first project in NetBeans.


  1. creating the HelloWorld project

    Creating a new NetBeans project.

    Steps to create the project:
    1. Run Netbeans.
    2. Click on the File menu, then select New Project.
    3. A new window will pop up. On the first page select "Java" from categories, and "Java Application" from projects. Then click next.
    4. Set Project Name to HelloWorld. Select a Project Location and Project Folder. These choices determine a directory where your work will be saved.
    5. Make sure Create Main Class is checked. Enter "HelloWorld" into the adjacent textbox. (This differs from the default.)
    6. The window should look similar to the one on this page. Click finish.
  2. Editing the HelloWorld class.

    screen shot of the helloworldapp package
    1. A window should be opened labeled HelloWorld.java.
    2. Enter the following code in the main function:
      System.out.println("Hello World!");
      
      Your screen should look as shown at right.
    3. Save the file using the menu: "File > Save".
    4. You should have noticed as you typed the period after System, NetBeans "helps" you. If you didn't notice this, play around with retyping it.
  3. Running the program.

    The following two methods both run the program:
    1. To run the program from within NetBeans do the following. Select "Run > Run Main Project" from the menu.

      A sub-window should pop up and display the output of your program. It should look something like:
      output of program

    2. To run the program from the command line (on a terminal) perform the following steps:
      1. Select "Run > Clean and Build Main Project" from the main menu bar.
      2. This has the effect of creating a ".jar" file. You can see the file by selecting the "Files" tab, then expanding down to the "HelloWorld/dist/" folder. See below, right.
        output of program
      3. In a terminal (or command prompt) outside of NetBeans, change directories to "HelloWorld/dist/" part of your project. You can run the program with the following command:
        java -jar HelloWorld.jar
  4. Discussion of Program.

    Source code with comments stripped out:
    public class HelloWorld {
    
        public static void main(String[] args) {
            System.out.println("Hello World!");        
        }
    }
    
    Comments:

Helpful Links:

Here are some links to some other "Hello World" tutorials:

Valid XHTML 1.0 Strict Valid CSS!

This presentation is part of a Mathematical Research Oriented Java Tutorial, which aims to introduce students to the benefits that writing computer programs can provide to their understanding of mathematics.