How to Run a Java Program in Terminal?

Java is the world-famous OOP-based language developed by James Gosling. It runs on multiple operating systems, including macOS, Linux, and Windows. When it comes to Linux, a Java program can be executed through the GUI and Terminal. This guide will demonstrate the possible methods to run a Java program through the terminal.

Prerequisite: How to Install Java in Terminal?

Java is a platform-independent and highly secure language compared to other programming languages. Before running a program, Java must be installed in the operating system. For this, the command is given below to install java in Debian operating systems:

For CentOS/RedHat Distributions:

$ sudo yum install java-11-openjdk-devel

For Arch Linux:

$ sudo pacman -S jre-openjdk

For Ubuntu/Debian Linux System:

$ sudo apt install default-jdk

How to Run a Java Program in a Terminal?

After the installation, you must confirm that the current system has enabled the Java Development Kit. To do so, the “version” utility is utilized to check the java compiler is installed in the operating system:

$ javac -version

The output shows that the current version of java compiler “javac 11.0.17” is installed in the Linux system.

Check Java RunTime Environment

To check the JRE is installed in the operating system, execute the below script:

$ java -version

The environment is ready to run the java program in the terminal.

(Optional) Install Java Compile

OpenJDK enables the compilation of Java applications. To install the latest version of the compiler, execute the below script:

$ sudo apt-get install openjdk-8-jdk

Java Program

You can open the “Text Editor” or “Nano Editor” and write the code based on your needs. In our case, the code is written in the text editor and saved with the name “program”.

class Student{
       public static void main(String[] args){
              System.out.println("Student are present in class");

The explanation of the code is given below:

  • A class Student is created on which the main function is defined.
  • In this function, a string “Student are present in class” is written to present in the output via “println()” method.

How to Compile Java Program Using File Name in Terminal?

To compile the java program from the name of the java file such as “program”, execute the below script:

$ javac

The output verifies that the program has been successfully executed by the name of the class.

How to Run Compiled Java Program Using Class Name in Terminal?

You can run the compiled program from the Ubuntu terminal by specifying the name of the class. In this case, the “Student” is the name of the class accessed through the terminal that will display the output:

$ java Student

The output “Student are present in class” confirms that the compiled java program has been successfully executed from the terminal.

Bonus Tip: How to Uninstall Java in Terminal?

It is an additional step to uninstall the java through the terminal. To do so, execute the below script to delete all dependencies associated with the installed java packages:

$ sudo apt remove --autoremove default-jdk

In this way, you can uninstall the java application from the Ubuntu operating system.


In the Ubuntu terminal, you can run the Java program by executing “javac <>” or “java <filename>”. Before execution, you must ensure the Java application is installed in the operating system. Users can execute the complex program in the terminal by following the same procedure. Additionally, the uninstallation method of java is provided here. This article has explained detailed guidelines for running the java program in the Ubuntu terminal.