Many new computer science students and new to programming young devs when exposed to a programming language. They use CLI tools to compile and run the program. So here is the basic guide on how to compile and run java files on Linux using the command line.

Java is one of the most popular programming languages out there, millions of the developers use it to develop various types of applications and services. It is most famously known for Android application development and industrial use. So now let’s take a demo on how to compile and run java files on Linux.

Creating a java program file

Before the compilation, We first need to write a program in java. Use any text editor you want and create a java program file.

You can also just copy and paste the code below into the editor and save the file as HelloWorld.java. We will use this file to demonstrate the process.

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

Compiling a java program file using the command line

Now open the terminal and type the below command in order to compile java program code into bytecode.

javac /path-to-file/HelloWorld.java

The above command will generate a HelloWorld.class file which is containing the bytecode. So to run the program just execute this class file into the JVM using the below command.

java HelloWorld

If everything is as per the plan then you will see the output as :

Hello World !!

This is how you can compile and run a java file using the command line. Let me know what you think about it in the comment section below.