Java Hello World Program

Simple Hello World Example in Java Programming

In this chapter, we will learn How to write and run the hello world – the simplest java program. Before we can run our first Java program or any other java program, we need some requirements on our machine. And we need some steps for running our program.

System setup & Steps before running Java

As we discussed in the Java introduction that Java is a compiled language. So before running Java, we have to set up the Java compiler on our machine. The default compiler of the Java program is the official JDK (Java Development Kit).

Then you need a text editor to write our Java program. Finally, we will compile and run our program.

To be precise;

  • Install the JDK, if you don’t already have installed. JDK is available here.
  • Create your Hello World program and name it Hello.java using a text editor.
  • Compile your program using javac command.
  • Finally, run the compiled program.

HINTS: The full Java Installation guide available here.

Java ProgramFlow

When you create a java file with the .java file extension, it is the main programable java file. When you compile it, the java file is converted into bytecode. The bytecode stored in the .class file. Finally, the bytecode trans compiled into the machine-readable executable code. See the diagram below;

java programflow

Writing Hello World Program

After successfully installed the JDK create a java file called Hello.java and write the following code in the file.

class Hello {  
    public static void main(String args[]){
     System.out.println("Hello, World!");
    }
}

To compile & execute this program;

  • Open up the command line in the current directory where you saved the Hello.java file.
  • javac Hello.java it will compile the program.
  • java Hello it will execute the program finally. And you will get the following output.

Output

Hello, World!

Possible Errors

Sometime you might get an error while compiling a java file “javac is not recognized as an internal or external command” even after installing the JDK.

This error occurs when your system has no Java SDK installed normally. In this case, install java SDK and try to compile your program again.

But if you get the same errors even after installing JDK, this means you need to set the Java environment path variable. To solve this refer to How to set up Java environment path variables.

What is next?

In the next chapter, we will learn some core basic java syntax by explaining the Hello World example that we just completed. It will make you sense How actually java program works.

Posted in