To start programming in Java on CentOS, you first need to install the (JDK) Java Development Kit on the system to compile and run a java program. We will demonstrate the installation of the JDK and Java program execution through the command line environment. To run a java program you need a text editor in which you will write a program such as sublime, Nano, or simple text file editor. First, you will compile the java program. After a successful compilation of a program a .class extension file is created and then you will use this file in order to execute the Java program.

In this article, you will learn how to write, compile, and run the first Java program. We have executed all the commands on the CentOS 8 system in this article.

First, you will log in as the root user on your system CentOS 8 and perform the following procedure:

Installation of the JDK (Java Development Kit)

Open the terminal window. Click on the ‘Activities’ from the left corner of your desktop and type terminal in the search bar. Click on the terminal icon from the search results as follows:


You can install JDK if it is not installed on your system CentOS 8. Use the following command to install Java on your system:

$ sudo dnf install java-11-openjdk-devel

Install Java JDK

You will wait for a while until the completion of the installation process.

Installation completed

To verify the installation of the process, you will check the version using the following command:

$ java -version

Check Java version

Java has been successfully installed on your system.

Now, your system is ready to create the first Java program on CentOS 8.

Create the first Java program

It is best practice to create a separate directory for all java programs. So, you will create a directory named ‘java_directory’ under the Home folder. Use the following command to create a directory:

$ mkdir java_directory

Using cd command you will navigate to this directory as follows:

$ cd java_directory

Create a directory

Now, you will create an empty file using the ‘touch’ command through the terminal. For this purpose, you also use the text editor. Execute the command in the terminal as follows:

$ touch

For example, here we have created a file named ‘’.

$ touch

Create Java source code file

Open the text editor and paste the following code in the above created file:

class helloworld {
  public static void main(String args[]){
    System.out.println("this  is my first hello world program");

Now, save the above text file with .java extension and close it as follows:

Create java file

This is a basic java program that will print “this is my first hello world program” on your terminal window. After writing the java program, you will compile it by using the java compiler. The syntax of program compilation is as follows:

$ javac

Here, the name of the file is’. So, the command would be as follows:

$ javac

After executing the above-mentioned command, the java compiler creates a class file that you will verify using the ‘ls’ command as follows:

Compile Java file

Now you will run the compiled java program using the syntax as follows:

$ java helloworld

You will see that the text “this is my first hello world program” has been printed on the terminal screen.

Run Java application

Congratulation! You have executed your first java program on CentOS 8.


In this article, you have learned how to install and execute your Java program on CentOS 8 through the command line.  Now, you are able to run any Java program using the terminal on your system CentOS 8. Moreover, you can explore how to set the path of java files.  I hope this article would be useful for you. Please give us your feedback via comments.

How to create your first Java program on CentOS 8

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.