How to Compile and Run your First Java Program

This tutorial will teach you how to write, compile and run your first Java program.


Read the following tutorial to learn how to install and set Java on your local environment → Setting Up Java Development Environment.

Hello World Program in Java

Open Notepad or any other text editor and type the following code:

1  public class HelloWorldClass {
3    public static void main(String[] args) {
4     System.out.println("Hello World!");
5    }
6  }

Save this file as The file name must be the same as the class name you created above.

Let’s see what each line of code contains and what precisely these keywords mean:

Line 1:

  • public – This is one of the Java access modifiers. Public means that the class will be visible within the entire program structure.
  • class – keyword is used to declare a class in Java.
  • HelloWorldClass – This is a class name; we could give this class any other name.

Line 3:

Here we have a declaration of the method. 

The main() method is the entry point into the application.

Its declaration contains:

  • public – This is one of the Java access modifiers. Public means that the method will be visible within the entire program structure.
  • static – This is one of the Java keywords. The static method in Java is a method that belongs to the class and not to the object.
  • void – It is the return type of the method. It means it doesn’t return any value.
  • main – This is the name of the method. For a class to be executed, it must contain the main() method.
  • String[] args – This is a parameter of the main method. Represents a command-line argument.

Line 4:

Here we added the body of the method. Let’s see what this code means:

  • System.out.println() – is used to print statements. The System is a class, out is the object of PrintStream class, and println() is the method of PrintStream class. 
  • “Hello World!” – We passed these words as a method parameter, and the println() method will print exactly that.

Note: The curly braces in Java are used for the code blocks.

On line 1, immediately after the class name, we have an open curly brace that marks the beginning of the class. In line number 6, we have a closing curly brace, and that’s how we know that’s where the class ends.

The same goes for methods.

On line 3, we have an open curly brace that marks the beginning of the block in which the method’s body is located, and on line 5, we have a closed curly brace which means that this is where the method ends.

Compile and run Java program

Compile the program

Open the command prompt or terminal, and go to the location where you saved the file.

To compile, we use the javac command, javac is a java compiler that we will call by executing this command, and as an additional parameter, we will put the name of the java file. So just type in javac and press Enter to execute the command.

After execution, you will notice that a new file with the same name but with a .class extension is created. A file with a .class extension is obtained after successfully compiling a .java file.

Run the program

To run the obtained HelloWorldClass.class file, you need to run the java command and, as an additional parameter, pass the file name. You don’t need to put in an extension this time.

Type in java HelloWorldClass end press enter.

As a result, you should see a printed Hello World! This is what it should look like:

how to run java program

You successfully execute your first Java program if you get the same output.

To learn more, check out other Java tutorials for beginners

Leave a Reply

Your email address will not be published.