How to Run a Command-Line Java Program on Linux?

Run A Java Program Featured

In this tutorial, we will learn about how to run a command-line Java program from the Linux terminal.

Prerequisites

To run Java programs from the command line, a functional Java Runtime Environment is required.

To install Java Runtime Environment on Ubuntu/Debian based systems:

sudo apt install default-jdk

and to install Java Runtime Environment on CentOS/RHEL based systems:

sudo yum install java-1.8.0-openjdk

To check if a functional Java Runtime Environment is available on your system execute:

java --version
Java Version Command Output
java –version command output

The output similar to shown above tells us about a functional Java Runtime Environment available on the system.

Running a Command-Line Java Program in Linux

Follow the steps listed below to get the bytecode from the source file, and to run the Java program.

Step 1: Write the Java program

You can use any text editor (like nano, vim, etc.) to write a Java program and then save it as a .java file. In this tutorial, we have written a “Hello World” Java program with the following source code, saved as a HelloWorld.java file.

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

Note: The Java source code file name (ending with extension .java) should be the same as the public class name (in here HelloWorld) of the Java program source code. That is why we have named our source code file HelloWorld.java

HelloWorld Java Program
HelloWorld Java Program

Once we have written and saved our Java program in a .java file, we can now proceed further.

Step 2: Compile your Java program

A Java compiler compiles the source code of a Java program to obtain a bytecode for the particular source code. To compile a Java program source code, the javac command is used.

javac FileName.java
Compiling Java Program command-line Java
Compiling Java Program

If no error is printed on the screen, then the Java program has been successfully compiled and a new file is obtained (in here the HelloWorld.class is obtained), which is the bytecode file for our Java program.

Obtaining Bytecode command-line Java
Obtaining our “Hello World” Java program bytecode file (HelloWorld.class)

Step 3: Run the command-line Java program

Now to run the Java program that we have compiled earlier in the second step.

We use the java command to run the Java program. To run the Java program, execute:

java FileName
Running A Java Program From Command Line command-line Java
Running a Java program from the command line

Notice that we haven’t used any file extension while running a Java program using the java command. This is because the Java command can identify the compiled Java files and does not need the .java extension to be added.

Conclusion

You can easily write, compile, and run a Java program from the command line following the steps mentioned above. To run a Java program, the Java Runtime Environment is required which can be installed using your Linux distribution’s official package manager.

Thank you for reading!