We have already seen how to install Java (Oracle-JDK) on Windows 10. Like that, you can also install Java on your Linux systems too. In this article I am going to show you how to install Java (Open-JDK 14) on Ubuntu 20.04 LTS Linux system.
You can also install Java on all Debian based Linux systems by following the steps used in this tutorial. Here we are going install Open-JDK as our Java platform, which is an open source GPL licensed software.
Installation Prerequisites –
- You need to have a root user or sudo user password
- Your system must have an internet connection
1. Update Ubuntu repositories
As regular, update your Ubuntu Linux system repositories before installing any software. It assures that you'll get the updated version of the software from Ubuntu repositories.
Open the Linux terminal by using keyboard shortcut Ctrl+Alt+T and hit the following command.
$ sudo apt update -y
After you have updated Ubuntu repositories successfully, give the following command to install Java (OpenJDK 14) on Ubuntu 20.04 system.
$ sudo apt install openjdk-14-jdk -y
Wait for some time till it fetches all the Java package files from internet. It’s about 450 Mb in size and will take time as per internet speed available on your system. Once it’s done, you will be back to $ prompt again.
Now Java is installed on your Ubuntu 20.04 system, check once, if it’s installed properly or not.
For that, hit the following command to confirm it. It will return the Java JDK version installed on your system.
$ javac –version
You can also check the installation location of Java using whereis javac command.
On my system it’s installed at the location /usr/lib/jvm/java-14-openjdk-amd64/bin.
2. Write a Java program
Now it’s time to create and run a small Java program on your Ubuntu 20.04 system.
Instead of using any Java IDE, we will use GEDIT text editor to write the program. And then we will compile and run the Java program using Linux command line using javac command.
$ gedit HelloWorld.java &
It will open GEDIT text editor with HelloWorld.java file. You have to just type the Java code in there, save and close it.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
You must match the Java class name with your Java program file name. Also your Java program file extension must be .java, e.g. in above program class name is HelloWorld and program file name is HelloWorld.java.
Once you finished writing your Java program, close GEDIT text editor and come back to the Linux command line.
3. Compiling a Java program
On the command line check the file you just created in your HOME directory using ls command. If you see your file HelloWorld.java, then proceed to compile it using following command.
$ javac HelloWorld.java
Here, same as all programming languages do, if your program is having errors, then it will be shown.
You have to remove those errors and try compiling the program again. If the Java program is error free, then it will be compiled successfully.
After successful compilation of Java program, it creates an Byte Code file. It can be executable by JVM (Java Virtual Machine) on any operating system including your Windows, Mac or Linux system.
4. Running a Java program
Now run ls command again to check the Java Byte code file created after compilation process. You will see a .class extension file is created there, named HelloWorld.class.
Run the following command to execute the Java program or execute the Java Byte Code.
$ java HelloWorld
You will see our Java program output on the terminal, printed as ‘Hello World!’. This way you can create Java programs furthermore as you go.
Conclusion
In this way we have seen how to install Java (OpenJDK-14) on Ubuntu 20.04 LTS Linux system.
As you know Java is the main app building platform on Android (Linux) system, so doing Java programming on Linux system will be more beneficial for you.
In this tutorial, we first installed Java (Open-JDK 14) on Ubuntu 20.04 Linux system. Then we created a simple Java program using GEDIT text editor rather than using any Java IDE.
Then, we compiled and run our Java program on Linux command line.
This way we have learnt to do Java programs on Linux command line using simple text editors.
GEDIT is a simple and widely used text editor of Linux systems, just like your NOTEPAD on Windows systems.
Happy Coding.










Comments
Post a Comment