Java hello world program

“Drive slow and enjoy the scenery – drive fast and join the scenery” Douglas Horton

The purpose of this section is to simply compile and run a program just to see how this process is done.

  1. Open a Notepad in your system.

  2. We need a source code to run. So for now just copy and paste the
    code below in the Notepad:

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

Suffice it to say, the program above simply sends the message “Hello World” to the screen.

Basically when you ran the program, you’ll see the message on your screen.

Now, save the file with the name Main and the extension of .java. So at the end you’ll have a file named Main.java in your program.

Note: because name of the class was Main we need to set a file with the same name. But if the name of the class was something else, we would need to change the name of the file to match the name of the class.

Also all Java source codes have the .java extension.

  1. Now open the command prompt and go to the directory where your
    file is stored and then type this command:

javac Main.java

G:hackjavaMy Java Tutorial3-Java Quick Example�1.png

Note: javac means Java-compile and it’s a command to signal the Java compiler to compile and produce a bytecode from our source code. (The name of the source code file sits on the right side of the command)

  1. At this time, our source code is compiled and ready to run. So
    while the command prompt is open and is in the same directory, we will
    type the command below:

java Main

G:hackjavaMy Java Tutorial3-Java Quick Example�2.png

If you see the message “Hello World” then congratulation! You’ve successfully ran your first Java program.