How to Run Java Programs in Linux Terminal

Run Java Programs in Linux

Java, a versatile and widely-used programming language, and Linux, a robust and reliable operating system, often go hand in hand in the world of software development. This article provides a comprehensive guide on how to run Java programs in a Linux terminal, a skill that can significantly enhance your productivity and understanding of both Java and Linux.


Before we delve into the process, ensure you have a basic understanding of Java and Linux. You should also have the Java Development Kit (JDK) installed on your Linux system. If you don’t have a Java program ready, don’t worry. We’ll create a simple one for demonstration purposes.

Setting Up the Java Environment in Linux

Checking if Java is Installed

First, we need to check if Java is already installed on your system. Open the terminal and type the following command:

java -version

If Java is installed, the command will output the version of Java currently in use. If not, you’ll need to install it.

Installing Java

To install Java, use the following command:

sudo apt install default-jdk

This command installs the default Java Development Kit (JDK) from the Linux package repository.

Setting JAVA_HOME Environment Variable

The JAVA_HOME environment variable points to the directory where Java is installed. To set it, open the /etc/environment file in a text editor and add the following line:


Save the file and reload the environment variables with the command:

source /etc/environment

Compiling and Running a Java Program in Linux Terminal

Creating a Java Program

Let’s create a simple Java program. Open a text editor, type the following code, and save the file as

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

Compiling the Java Program

To compile the Java program, use the javac command followed by the name of your Java file. Like so:


This command generates a HelloWorld.class file, which is the bytecode version of your program.

Running the Java Program

To run the compiled Java program, use the java command followed by the name of the class file (without the .class extension):

java HelloWorld

The program will output: Hello, World!.

Troubleshooting Common Issues

Dealing with “Command not found” Error

If you encounter a “Command not found” error when trying to run java or javac, it means that the system cannot find the Java executables. This issue can often be resolved by reinstalling Java or correctly setting the JAVA_HOME environment variable.

Resolving Classpath Issues

If you get a “Could not find or load main class” error, it means that Java cannot find the class file. This issue can be resolved by ensuring that you’re in the correct directory and that the classpath is set correctly.

Handling Java Version Conflicts

If you have multiple versions of Java installed, you might encounter conflicts. You can manage different versions of Java using the update-alternatives command in Linux.

Advanced Topics

Running Java Programs with Command-Line Arguments

Java programs can accept command-line arguments. These arguments are passed into the main method as an array of String objects.

Using External Libraries in Your Java Program

Java programs can use external libraries, which should be included in the classpath when compiling and running the program.

Automating the Compile and Run Process with a Bash Script

You can automate the process of compiling and running a Java program using a Bash script. This script can include commands for compiling the Java program and running the resulting class file.


Running Java programs in a Linux terminal is a fundamental skill for any Java developer working in a Linux environment. This guide has provided a comprehensive overview of the process, from setting up the Java environment to compiling and running a Java program, and even troubleshooting common issues. With this knowledge, you’re well-equipped to effectively develop and run Java programs in Linux.


r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button