You like what you Read then Vote!
0 / 5 4.86 21

Your page rank:

In our last tutorial, we discussed some of Java terminologies like Java virtual Machine(JVM), Java runtime environment(JRE) and Java development kits(JDK).

If you have not read the previous tutorial, I will suggest your read it before you continue with this tutorial

We are going to write our first Java code today. I can see smile. If you are a Java beginner and have no previous programming experience, it is important you follow our java tutorials in chronological order.

By the time you are done with our basic Java tutorials, you will be sound enough to write simple java programs.

In most programming language tutorials, the first program use to introduce beginners to a programming language is the classical hello word program.

Below is our first java program that outputs hello word to the screen.

public class FirstJavaProgram {
  public static void main(String[] args){
      System.out.println("Hello World");
  }//End of main
}//End of FirstJavaProgram Class
Output: Hello Word


Prerequisite: You need to have java installed on your system. You can get the java from here.


Open a text editor, like Notepad on windows and TextEdit on Mac. Copy the above program and paste it in the text editor.

You can also use IDE like Eclipse to run the java program but we will cover that part later in the tutorials.

For the sake of simplicity, I will only use text editor and command prompt (or terminal) for this tutorial


Save the file as

You may be wondering why we have named the file as FirstJavaProgram, the thing is that we should always name the file same as the public class name.

In our program, the public class name is FirstJavaProgram, that’s why our file name should be


In this step, we will compile the program. For this, open command prompt (cmd) on Windows, if you are Mac OS user then open Terminal.

To compile the program, type the following command and hit enter.

javac FirstJavaProgram. Java

You may get this error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file“.

This error occurs when the java path is not set in your system

If you get this error then you first need to set the path before compilation.


Open command prompt (cmd), go to the place where you have installed java on your system and locate the bin directory, copy the complete path and write it in the command like this.

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

Note: Your jdk version may be different. Since I have java version 1.8.0_121 installed on my system, I mentioned the same while setting up the path.


Open Terminal, type the following command and hit return.

export JAVA_HOME=/Library/Java/Home

Type the following command on terminal to confirm the path.


That’s it.

The steps above are for setting up the path temporary which means when you close the command prompt or terminal, the path settings will be lost and you will have to set the path again next time you use it.

I will share the permanent path setup guide in the coming tutorial.


After compilation the .java file gets translated into the .class file(byte code). Now we can run the program.

To run the program, type the following command and hit enter:

java FirstJavaProgram

Note that you should not append the .java extension to the file name while running the program.


Now that we have understood how to run a java program, let have a closer look at the program we have written above.

public class FirstJavaProgram {

This is the first line of our java program.

Every java application must have at least one class definition that consists of class keyword followed by class name.

When I say keyword, it means that it should not be changed, we should use it as it is.

However the class name can be anything.

I have made the class public by using public access modifier, I will cover access modifier in a separate post.

All you need to know now that a java file can have any number of classes but it can have only one public class and the file name should be same as public class name.

public static void main(String[] args) {

This is our next line in the program, lets break it down to understand it:

public: This makes the main method public that means that we can call the method from outside the class.

static: We do not need to create object for static methods to run. They can run itself.

void: It does not return anything.

main: It is the method name. This is the entry point method from which the JVM can run your program.

(String[] args): Used for command line arguments that are passed as strings. We will cover that in a separate post.

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

This method prints the contents inside the double quotes into the console and inserts a newline after.


Before now, we have explain how to compile and run java program using the command line prompt. 

Although it is not quite simple but as you learn more java basics and start writing simple programs, you will come to find out that using a Java IDE will make your life much easier.

There are many Java IDE’s out there (Some are free while others are paid).

These are Java IDE you will always come across – Eclipse, NetBeans and Intellij.

We are going to use Eclipse in our future tutorials. Feel free to use any IDE of your choice.


To install Eclipse in your system, go over to their official website here.

  1. Click on the download button
  2. Go to the location where eclipse installer was downloaded and double click the .exe file to initiate download.
  3. Select Eclipse IDE for Java Developers
  4. Choose an installation folder
  5. Read the user agreement. Accept or decline it depending on your choice.
  6. Now that we have finish with the installation, you can click on the launch button to start eclipse application as show below.

Once Eclipse IDE is loaded we will get an interface that looks similar to the image below. Please note that the UI might depend on the version of Eclipse IDE your are using.


In the Eclipse welcome page as shown in the image above, click on Create a Hello Word Application. 

Follow the cheat sheet at the right hand panel to create the application.

Another option is to create a new Java project by going to  File > New > Java Project.

Fill in the detail information about your Java project. Remember to add a package name rather than using the default package name.

Click finish button when you are done.

Create a java class file inside the source directory of your project by right clicking on the folder.

Go to New > class and fill in your class name. Click the finish button when you are done.

Copy the code above and run the program by clicking on the green circle icon menu.

The program output is displayed in the console panel of Eclipse as shown below.


We have gone through how to create our first ever java program. In the process, we learn how to compile and run java code using command line interface.

We went further to illustrate the importance of IDE like Eclipse and how it simplifies code editing, compiling, error management, debug and running of java application.

If you have any questions or suggestions, kindly use the comment box below or you can contact me through my email address.

In our next tutorial, we will look at what is variables in Java.

Recent Articles

Related Stories

Stay on op - Ge the daily news in your inbox