How to Run Java Programs in Linux Terminal?

Running Java or Java Programs within a Linux Terminal can be daunting. But worry not, as we have got you covered.

Java is a popular programming language used to build all types of applications. With its object-oriented approach and easy-to-understand syntax, Java is considered one of the best programming languages to start with and grow with. It is supported by Linux, macOS, and Windows.

However, most Linux users prefer to do things on the terminal, and running Java Programs on the terminal is no exception. This guide will demonstrate the steps to set up a Java environment on Linux so you can easily compile and run Java Programs on the terminal without any complications.

How to Run Java Programs in Linux Terminal?

To run Java Programs within the Terminal, you must ensure that the JDK has been installed on your machine. After that, the present working directory of the Terminal should contain the Java program you are trying to run. Lastly, you must use the “java filename” command in the terminal

However, the Java program must be compiled using the Java Compiler Command (javac) before being executed through the terminal.

Let’s go over the step-by-step procedure.

Verify the JDK Installation

Linux cannot compile or execute Java programs without the Java Development Kit. Therefore, the first step is to ensure the JDK has been installed on your machine. To verify this, open up a terminal (CTRL + ALT + T) and type in the following command inside it:

javac -version

Running this command will show you the current version of the installed JDK on your machine. But, if the command returns an error like the following.

This output means that the JDK is not installed on your Linux machine, and you must install it first.

Install JDK in Linux

To install the JDK on Linux, open a new terminal and type the following command.

sudo apt update

This command will apt repositories of your Linux. Once that is done, run the following command to download and install JDK from the apt repository.

sudo apt install default-jdk

Once you execute the command, the terminal will prompt you to provide the password for the root (sudo) user. Therefore, type that in and press the Enter key.

After that, you will get a confirmation prompt on the terminal. Type “Y” and press the Enter key.

After that, wait for the downloading and installation process to complete fully. Once the installation is done, you need to verify the correct installation of JDK. To do this, open up a new terminal and type the following command.

javac -version

The output shows that the JDK has been successfully installed.

Write a Java Code File

You need a code file written in Java that can be compiled and then executed on the terminal as a Java Program. If you do not have a Java file, then simply create one. Let’s create a file named “demo.java” on the desktop using the command “touch demo.java.”

This file contains a basic Java code that prints “Hello World” on the console.

Once you have written the code inside the file, press the save button on the top right before exiting the file. Additionally, ensure that the class name is the same as the file name you created.

Compile the Java Program Using the “javac” Command

The next step is to compile the program using the Java compiler command. To do this, open a new terminal session and head over to the directory that contains the Java Program, Which is the desktop for this tutorial, and type “ls” to check for the file.

After that, use the following command template: “javac filename.java.” Which, in this case, will become “javac demo.java.” Doing this will create a new file in the same directory as “demo.class”.

Run the Java Program Using the “java” Command

Once you have the Java Program, you only need the “java filename” command to run it through the terminal. Therefore, open a terminal, navigate to the directory of the Java Program, and then run the following command:

java demo

Running this command will generate the following output on the terminal:

The output shows that the Java Program was successfully executed through a Linux Terminal. However, remember that you don’t have to add the “.class” part of the file while running the Java Program.

Affordable VPS Hosting With Dracula Servers

Looking for reliable and budget-friendly Virtual Private Server (VPS) hosting? Look no further than Dracula Servers. Dracula Servers offers a range of VPS hosting plans tailored to meet diverse needs. With competitive pricing, robust performance, and a user-friendly interface, it’s an excellent choice for individuals and businesses alike.

Explore the Dracula Servers website to discover hosting solutions that align with your requirements and take your online presence to new heights with their affordable and efficient VPS hosting services.

Visit Dracula Servers and experience reliable VPS hosting without breaking the bank.

How to Install JDK On Different Linux Versions?

The commands to install an application can vary with the version/flavor of Linux you are running, which is also true when you want to install JDK on your Linux machine. However, the steps are the same: Update the Linux and run the respective installation command.

To install JDK on Ubuntu, run the following commands.

sudo apt-get update
sudo apt-get install default-jdk

To install JDK on Fedora, run the following commands.

sudo dnf update
sudo dnf install java-11-openjdk-devel

To install JDK on CentOS, run the following commands.

sudo yum update
sudo yum install java-11-openjdk-devel

To install JDK on Arch Linux, run the following commands.

sudo pacman -Syu
sudo pacman -S jdk-openjdk

Once the installation is done, you can verify it using the command “java -version.”

How to Run JAR File in Linux Terminal?

To run a JAR program on Linux through a terminal, make sure that you have JDK/JRE installed on your Linux machine and the path variables are configured. Once that is done, open a terminal and head to the directory of the JAR file. Then run the command “java -jar [filename].jar

The main difference in running Java Programs and Java Applications (JAR files) is adding the “-jar” flag. However, if the class is not specified within the Java application’s Manifest file, you must use the “-cp” instead of the “-jar” flag and specify the entry class in the command.

Conclusion

Running a Java Program through a terminal is a rather simple process. However, if you are doing it for the first time, then it requires you to perform a series of steps. These steps include ensuring the installation of JDK in your Linux machine, compiling the Java code into a Java Program, and then running the Java Program through the terminal.

If you are only looking for the commands to run the program in the terminal, then use “javac [filename].java” to compile a java code into an executable program and the “java [filename]” to execute the program on the terminal.

Check out More Linux Tutorials Here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
× Dracula Servers

Subscribe to DraculaHosting and get exclusive content and discounts on VPS services.