Creating Your First Java Program

Format
mla apa chicago
Your Citation
Leahy, Paul. "Creating Your First Java Program." ThoughtCo, Feb. 28, 2017, thoughtco.com/creating-your-first-java-program-2034124. Leahy, Paul. (2017, February 28). Creating Your First Java Program. Retrieved from https://www.thoughtco.com/creating-your-first-java-program-2034124 Leahy, Paul. "Creating Your First Java Program." ThoughtCo. https://www.thoughtco.com/creating-your-first-java-program-2034124 (accessed September 21, 2017).
01
of 08

Java Basics

Printing Hello World to the Computer Screen
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Note: Before you start this tutorial, you must have downloaded and installed the Java SE Development Kit.

This tutorial introduces the basics of creating a very simple Java program. When learning a new programming language, it's traditional to start with a program called "Hello World." All the program does is write the text "Hello World!" to the command or shell window. 

The basic steps to create the Hello World program are:

  1. Write the program in Java
  2. Compile the source code
  3. Run the program

02
of 08

Write the Java Source Code

Program Code in Notepad
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

All Java programs are written in plain text — therefore you don't need any special software. For your first program, open up the simplest text editor you have on your computer, likely Notepad.

The entire program looks like this:

 // The classic Hello World! program                        //1
 class HelloWorld {                                         //2
 
    public static void main(String[] args) {                //3
 
       //Write Hello World to the terminal window
       System.out.println("Hello World!");                  //4
 
    }                                                       //5
 }                                                          //6

While you could cut and paste the above code into your text editor, it’s better to get into the habit of typing it in. It will help you learn Java more quickly because you will get a feel for how programs are written, and best of all, you will make mistakes! This may sound odd, but each mistake you make helps you to become a better programmer in the long run. Just remember that your program code must match the example code, and you’ll be fine.

Note the lines with "//" above.  These are comments in Java, and the compiler ignores them.

The Basics of This Program

  1. Line //1 is a comment, introducing this program.
  2. Line //2 creates a class HelloWorld. All code needs to be in a class in order for the Java runtime engine to run it. Note that the entire class is defined within enclosing curly braces (on line /2 and line //6).
  3. Line //3 is the main() method, which is always the entry point into a Java program. It also is defined within curly braces (on line //3 and line //5). Let's break it down:
    public: This method is public and therefore available to anyone.
    static: This method can be run without having to create an instance of the class HelloWorld.​
    void: This method does not return anything.
    (String[] args): This method takes a String argument.
  4. Line //4 writes "Hello World" to the console.

03
of 08

Save the File

Save the File
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Save your program file as "HelloWorld.java". You might consider creating a directory on your computer just for your Java programs.

It’s very important that you save the text file as "HelloWorld.java". Java is picky about filenames. The code has this statement:

class HelloWorld{

This is an instruction to call the class "HelloWorld". The filename must match this class name, hence the name "HelloWorld.java". The extension ".java" tells the computer that it’s a Java code file.

04
of 08

Open a Terminal Window

Run Dialog Box
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Most programs that you run on your computer are windowed applications; they work inside a window that you can move around on your desktop. The HelloWorld program is an example of a console program. It does not run in its own window; it has to be run through a terminal window instead. A terminal window is just another way of running programs.

To open a terminal window, press the "Windows key" and the letter “R”.

You will see the "Run Dialog Box". Type "cmd" to open the command window, and press "OK".

A terminal window opens on your screen. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files they contain, and run programs. This is all done by typing commands into the window.

05
of 08

The Java Compiler

Set the Compiler Path
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Another example of a console program is the Java compiler called "javac." This is the program that will read the code in the HelloWorld.java file, and translate it into a language your computer can understand. This process is called compiling. Every Java program you write will have to be compiled before it can be run.

To run javac from the terminal window, you first need to tell your computer where it is. For example, it might be in a directory called "C:\Program Files\Java\jdk\1.6.0_06\bin". If you don’t have this directory, then do a file search in Windows Explorer for "javac" to find out where it lives.

Once you’ve found its location, type the following command into the terminal window:

 set path= *the directory where javac lives*

E.g.,

 set path=C:\Program Files\Java\jdk\1.6.0_06\bin 

Press Enter. The terminal window will just return to the command prompt. However, the path to the compiler has now been set.

06
of 08

Change the Directory

Change the Directory
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Next, navigate to the location your HelloWorld.java file is saved. 

To change the directory in the terminal window, type in the command:

cd *directory where HelloWorld.java file is saved*

E.g.,

cd C:\Documents and Settings\userName\My Documents\Java 

You can tell if you’re in the right directory by looking to the left of the cursor.

07
of 08

Compile Your Program

Compile Your Program
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

We’re now ready to compile the program. To do so, enter the command:

javac HelloWorld.java

Press Enter. The compiler will look at the code contained within the HelloWorld.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.

Hopefully, you should have no errors. If you do, go back and check the code you’ve written. Make sure it matches the example code and re-save the file.

Tip: Once your HelloWorld program has been successfully compiled, you will see a new file in the same directory. It will be called “HelloWorld.class”. This is the compiled version of your program.

08
of 08

Run the Program

Run the Program
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

All that’s left to do is run the program. In the terminal window, type the command:

java HelloWorld

When you press Enter, the program runs and you will see "Hello World!" written to the terminal window.

Well done. You’ve written your very first Java program!