In this tutorial, you will learn how to write a simple program to print the message “Hello World” to standard console output. You will learn how to compile a Java program, and how to run the compiled code.

“Hello World!” Program

As a prerequisite, let us make sure that our environment is ready to compile and run Java code. If Java is not installed, follow the instructions to Install Java in your computer.

Writing HelloWorld.java

Take a text editor of your choice (like notepad in Windows or gedit in Ubuntu) and write the program as following. Save the file with the name “HelloWorld.java”. The file name should match with the name that is mentioned after the class keyword, i.e., HelloWorld

HelloWorld.java

</>
Copy
public class HelloWorld{
	public static void main(String[] args){
		System.out.println("Hello World");
	}
}

Compiling HelloWorld.java

Compiling is the process in which Java Language converts the java statements to byte code. The byte code is understandable by the JRE(Java Runtime Environment) irrespective of the Operating System. This is what makes Java, platform independent.

To compile the Java program, navigate to the folder in which HelloWorld.java is present and run the following command (javac <JavaFileName.java>)

tutorialkart@java:~$ javac HelloWorld.java 

If compiling is successful without any errors, .class file is created. (In this case: HelloWorld.class)

tutorialkart@java:~$ ls
HelloWorld.class    HelloWorld.java

Running HelloWorld.class

Running the .class file loads the bytecode into Java Runtime Environment. JRE executes the instructions specified in byte code format. To run the .class file, run the following command from the location of .class file

tutorialkart@java:~$ java HelloWorld
Hello World

HelloWorld is printed on the console, in which case, we have written a program that could print a message to standard console.

Conclusion

In this Java Tutorial, we have successfully written, compiled and run the HelloWorld java program. In our next Java Tutorial, we shall learn about typical structure of a java program.