How to create your first Java program on CentOS 8

s2 0

To start programming in Java on CentOS, you must first install the Java Development Kit (JDK) on the system to compile and run a Java program. We demonstrate the installation of the JDK and the Java program execution from the command line environment. To run a Java program, you need a text editor in which to write a program like Sublime, Nano, or a simple text file editor. First you compile the Java program. When a program is successfully compiled, a .class extension file is created that you then use to run the Java program.

This article shows you how to write, compile, and run your first Java program. We ran all the commands on the CentOS 8 system in this article.

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

Installation of the JDK (Java Development Kit)

Open the terminal window. Click on “Activities” in the left corner of your desktop and type terminal in the search bar. In the search results, click the terminal icon as follows:

You can install JDK if you don’t have CentOS 8 installed on your system. Use the following command to install Java on your system:

$ sudo dnf install java-11-openjdk-devel

You will wait a while for the installation process to complete.

installation completed

To verify the installation of the process, check the version with the following command:

$ java -version

Check Java version

Java has been successfully installed on your system.

Now your system is ready to build the first Java program on CentOS 8.advertising

Create the first Java program

It is advisable to create a separate directory for all Java programs. So you create a directory called ‘java_directory’ under the home folder. Use the following command to create a directory:

$ mkdir java_directory

Use the cd command to navigate to this directory as follows:

$ cd java_directory

Create a directory

Now create an empty file using the ‘touch’ command from the terminal. You can also use the text editor for this. Run the command in the terminal as follows:

$ touch filename.java

For example, here we created a file called ‘helloworld.java’.

$ touch helloworld.java

Create Java source code file

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

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 a .java extension and close it as follows:

Create Java file

This is a basic Java program that prints “This is my first hello world program” in your terminal window. After you have written the Java program, compile it using the Java compiler. The program compilation syntax is as follows:

$ javac filename.java

Here the name of the file is helloworld.java ‘. So the command would be as follows:

$ javac helloworld.java

After executing the above command, the Java compiler creates a class file which you can check using the ‘ls’ command as follows:

Compile Java file

Now run the compiled Java program with the following syntax:

$ 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

Congratulations! You ran your first Java program on CentOS 8.

diploma

This article showed you how to install and run your Java program on CentOS 8 from the command line. Now you can run any Java program with the terminal on your CentOS 8 system. You can also explore how to set the path of Java files. I hope you find this article useful. Please give us your feedback via comments.

How to create your first Java program on CentOS 8