How to Run Java from Command Line in Linux

Wondering how to run Java from command-line in Linux?

Java is one of the premier programming languages of the world. Developed by the Oracle Corporation, it is a high-level Object Oriented Programming(OOP) language and was based on the concept of Write Once Run Anywhere (WORA). Although it is a mixture of C and C++, it does not possess the same low-level facilities available.

Java is a general-purpose language used for different objectives such as mobile application development, website development, gaming application development, cloud computing, etc. Therefore, having such a wide range of applications is one of the most popular and utilized programming languages. 

As stated before, it was based on WORA, so the programs and software based on Java can run on multiple operating systems. Other than that, you can code with Java on multiple operating systems as well. Linux is not an exception either. Users of Linux and its kernels can create programs using Java. 

If you are looking forward to learning how to run Java from command-line in Linux, you have come to the right place. This article will lead you to successfully write a Java program using the command terminal provided in Linux.

So buckle up, and let’s get started!

Step 1: Verify the Availability of JDK on Your System.

To learn Java from Command-Line in Linux, the first step is to check whether your computer has JDK or not. JDK, otherwise known as the Java Development Kit. For writing and compiling code in Java, the presence of JDK is essential.

We would be using Ubuntu as our operating system for this tutorial; however, the method is no different for any other Linux distribution. 

There is a simple command that you can use to check for JDK, and it can be typed and executed on the command Terminal available on every Linux Kernel. The command is:

$ javac -version

Follow these steps to check for JDK:

  1. Open the terminal application by pressing Ctrl + Alt +T
  2. Once it is open, type the command in it.
$ javac -version
  1. If you already have JDK installed, you should receive an output similar to this image, with the version of JDK mentioned in the terminal output.
Run Java from Command-line in Linux

In case your system does not have JDK installed in it, you should receive the following output.

  1. There are different ways to install JDK, as you can see in the image above. You can choose any command according to your preference. For this guide, we will go with the default command used to install JDK. 
$ sudo apt install default-jdk

When you press enter to execute the command, you can see the progress: 

  1. When you are notified that JDK has been successfully installed, recheck it by typing this command again
$ javac -version 

Now that you have verified the presence of the Java Development Kit on your system, we are one step closer to learning how to run Java from the command-line in Linux.

Step 2: Writing and Compiling a Program in Java.

As the Java Development Kit is now installed, you can now be on your way to writing a program in Java. There are different ways you can write and compile programs, but as we are sticking only to the Terminal, we will be using the GNU nano application.

GNU nano is the command-line text editor available on all Linux and Unix-based operating systems. Other text editors, such as Vim, are available, so it’s your choice which editor you want to use.

Now, as you will write a program in Java, you have to keep in mind that the name of the file containing the code should have the “.java” extension. Also, the name of the file should be the same as your class name. 

Let’s write a program then:

  1. Open the command terminal on your system.
  2. Type the following command in the terminal:
$ nano
Run Java from Command-line in Linux
  1. We named our file “First”. The class in the code will be named the same. 
  2. When the application opens, you can type in the program you want to write. As an example, we have chosen a simple program. 
Run Java from Command-line in Linux
  1. After you are done writing your program, save the program using CTRL+O and close the nano text editor.
  2. The next step is to compile the program. We will do this by using the same command we used to check the availability of JDK:
$ javac
  1. If there is no error in your code, no error message would be generated. If there is one, an error message such as this will be generated.
Run Java from Command-line in Linux

With this, you now know how to code in Java using the Terminal, and you know the necessary prerequisites to code in Java using the Terminal. With this, we move on to the final step.

Step 3: Running Java Code on Terminal.

The final piece of the puzzle is to learn how to run the program you have written in Java. To run the program, you need to use the command “java”. The syntax for this command is:

$ java <class name>

After the program has been compiled, there is no need to write a filename with the “.java” extension. 

To run the program you have just written, please follow these steps:

  1. Open the terminal if not already opened.
  2. Type the following command to run the program.
$ java <class name> 

For example, 

$ java first
  1. When you press enter, you see the output of the program on the screen of your terminal.
Run Java from Command-line in Linux

So, as you may have noticed, writing code in Java using the command Terminal is pretty simple and easy. 

This article was a step-by-step guide designed for people who want to learn how to run Java from command-line in Linux. The first step was to know whether you had the Java Development Kit installed on your system or not.

The second step was to write code in the application GNU nano, which is the command-line text editor available on Linux and its distributions. The second step also describes how to compile the program you have just written.

The third and final step was to run the program you had written on the command Terminal. We hope that this guide was able to describe each of these steps perfectly. The steps themselves were not complex at all. So, you can continue practicing and learning Java with the command Terminal on your Linux systems.

If you want to learn how to perform a reverse DNS lookup on Linux and its kernels, this article should help you out.                                       

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts