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: Follow these steps to check for JDK: In case your system does not have JDK installed in it, you should receive the following output. When you press enter to execute the command, you can see the progress: 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: 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: 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: For example, 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. 🙂