Compile and Run your First Java Program

You will discover a step-by-step tutorial for writing, compiling, and running your first Java application in this tutorial. A Java application to print “Hello World” on the screen will also be written.

Let’s begin with a straightforward Java application.

Simple Java Program

This is my first Java program, and it outputs the message “This is extremely simple.”

public class FirstJavaProgram {
  public static void main(String[] args){
    System.out.println("This is my first program in java");
  }//End of main
}//End of FirstJavaProgram Class


This is my first program in java

How to compile and run your first java program

Java must be set up on your system as a prerequisite.

Step 1: Open a text editor, such as Mac’s TextEdit or Windows’ Notepad. The program listed above can be copied and pasted in a text editor.

Step 2: Name the file and save it. You might be asking why we gave the file the name FirstJavaProgram, but the rule is that the file name should always correspond to the name of the public class. FirstJavaProgram is the name of the public class in our program, hence should be the file name for our application.

Step 3: We will compile the program in this stage. Open Terminal if you are using Mac OS, or Command Prompt (cmd) if you are using Windows for this.

Enter the command after typing it to compile the application.


If you get this issue, you must set the path before compiling.

Set Path in Windows:

Open the command prompt (cmd), navigate to the location where Java was installed on your machine, and find the bin directory. Copy the whole path and enter it as is in the command.

set path=C:\Program Files\Java\jdk1.8.0_121\bin

Note: Possibly, your jdk version is different. I noted this while setting up the route because my machine is running Java version 1.8.0 121.

Set Path in Mac OS X:

Enter the following command in Terminal, then press Return.

export JAVA_HOME=/Library/Java/Home

To verify the route, enter the following command into the terminal.


 Step4: file is converted into a.class file after compilation (byte code). We may now execute the program. Enter the following command to launch the program:

java FirstJavaProgram

Note that while executing the application, you shouldn’t add extension to the file name.

How the First Java Program works?

After learning how to execute a Java program, let’s take a closer look at the program we wrote earlier.

public class FirstJavaProgram {

Our Java program’s first line begins with this. At least one class definition, consisting of the class keyword followed by the class name, must be included in every Java program. When I use the term “keyword,” I mean that it should remain unchanged and be used exactly as is. The class name, however, can be anything.

A Java file can include any number of classes, but it can only contain one public class, and the file name must be the same as the public class name. I made the class public by using the public access modifier; I will address access modifier in a subsequent piece.

public static void main(String[] args)  {

Our program’s next line is as follows; in order to comprehend it, let’s split it down:

public: The main function is now public, allowing us to invoke it from outside the class.

static: For static methods to function, we don’t need to build an object. They are self-sustaining.

void: It gives no results back.

main: the name of the procedure. The JVM can run your program from this entry point technique.

(String[] args): used for parameters supplied as strings on the command line. That topic will be covered in a different post.

System.out.println("This is my first program in java");

This technique adds a newline after printing the string enclosed in double quotes on the console. This is something that I went into great length about in my System.out.println tutorial ().

Java “Hello, World!” Program

The most typical query is this one. The first program everybody learns to develop in Java is to produce the phrase “Hello World!” on the computer screen. This software is identical to the one we have already seen.

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


Hello World!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button