How to Install Java in Linux?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Java is one of the most widely used programming languages. To execute code in any programming language, we need to set up required environments like code editors, interpreters, compilers, and IDEs. JDK (Java Development Kit)' is the complete environment required to run java codes on any system. IDE(Integrated development environment) like Eclipse and IntelliJ makes coding and developing complex projects easier with its user interfaces, automation tool, debugger, and many other features.

Introduction

Java is a high-level, robust programming language and a platform. Developed by James Gosling at Sun Microsystems that was released in 1995. Any hardware or software environment in which a program runs is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

To run any code and develop applications in java language, we need to install Java Development Kit(JDK). JDK contains tools required for development, and JRE(Java Runtime Environment), which contains all libraries and JVM(Java Virtual Machine), is required to execute the programs.

JDK can be installed on different operating systems, including Linux, windows, and macOS. Here, we will see detailed steps of installation of JDK SE 16 on the Linux operating system.

Which One do I need: JVM or a JRE, or a JDK?

JVM, JRE, and JDK are different software bundles available for running java programs with different functionalities. To understand what we need to install JVM, JRE or JDK, let's understand these terms.

1. Java Runtime Environment(JRE)

  • Java Runtime Environment (JRE) is an open source software distribution with a Java class library, specific tools, and a JVM.
  • It is the most common environment for devices running Java applications. JRE can be used when a user has to use Java applications but does not do development.
  • Java source code is compiled and translated into Java **bytecode**. If you want to get output from this bytecode, you need JRE.
  • It loads classes, checks memory access, and finds the system resources needed to run applications.
  • JRE acts as a software layer over the operating system. It is one of the related components of Java Development Kit(JDK) and a superset of JVM.

2. Java Virtual Machine (JVM)

  • JVM(Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which java bytecode can be executed.
  • When we run any Java program, the online Java compiler first compiles your Java code into bytecode. After compilation, JVM translates bytecode into machine code, i.e., machine-level instructions.
  • Java is known as a platform-independent language because programs written in Java are executed by JVM and not by the physical machine.
  • To execute any Java program user has to first compile the code to convert Java code into byte code and then run it. The compiler creates a .class file after successful compilation.
  • After compilation, the byte code given in the .class file by the compiler is converted into machine-level instructions by JVM, and output is given to the user.

Syntax to compile Java program:

Syntax to run Java program:

Here Java command activates JVM.

  • Flow of program execution is done as follows: Java command activates JVM

3. Java Development Kit(JDK)

  • JDK is a software development kit required to develop applications in Java.
  • JDK consists of development tools like compiler, and debugger in addition to JRE.
  • It contains JVM, JRE, and additional development tools like a compiler, interpreter, debugger, and many more things required for application development.
  • JDK makes it easy for developers to develop applications.

Java Development Kit

Points to Remember while Downloading:

  1. If you're a user looking to run a Java application, you only need the JRE, which includes a JVM and libraries that will run java programs successfully.
  2. If you're a developer looking to program in Java and use any IDE for the same, you need the JDK (which includes JRE libraries, which in turn includes a JVM.)

From the above explanations, we know that JDK contains all the required components(JVM, JRE, compiler, and debugger) to execute a Java program. So, we will download and install JDK 16.

NOTE: Here, we will install default versions available in the repository of the Linux operating system. UbuntuOS in my case.

Difference between JRE vs OpenJDK vs Oracle JDK

JDK is a software development kit required to develop applications in Java. JDK consists of development tools like a compiler, debugger, etc. in addition to JRE. While installing JDK in Linux, there are two options OpenJDK and Oracle JDK.

Both, OpenJDK and Oracle JDK are java development kits and have all the software that is present in a JDK. But there are some key differences between OpenJDK and Oracle JDK.

Java Runtime Environment(JRE)

Java Runtime Environment (JRE) is an open-source software distribution with a Java class library, specific tools, and a JVM. It is the most common environment for devices running Java applications. JRE can be used when a user has to use Java applications but does not do development. JDK consists of JRE and JVM.

OpenJDK:

OpenJDK (Open Java Development Kit) is an open-source implementation of the Java Platform Standard Edition. It is free to use with General Public License Version2 and was launched by Sun Microsystems in 2006.

Oracle JDK:

Java programming language was originally developed by Sun Microsystems but it is now owned by Oracle. Oracle JDK is also owned by Oracle. Oracle started charging for Java SE 8 licenses in 2019. Hence, Oracle JDK is chargeable.

OpenJDK Vs Oracle JDK

  • The key difference between OpenJDK and Oracle JDK is the licensing process. OpenJDK is open-source Java with General Public License.
  • Oracle JDK requires a paid license under the Oracle Binary Code License Agreement. But there are many other changes in support and cost.
  • OpenJDK has the same code as OracleJDK, depending on which provider you are using.
  • Oracle JDK performance is better than OpenJDK, but that is changing. OpenJDK performance is constantly improving.

Note: Default JDK for Linux is OpenJDK, and we will install the same.*

How to Check if Java is Already Installed in Linux?

Step 1: To check if java is already installed in your Linux system, open the terminal and run the command java -version.

Step 1

Step 2: If it is already installed, it will show the installed version java. Else it will show Java not found.

Step 2 Here java is not installed on the Linux machine. So, let's install it.

How to Download and Install Java in Linux?

Step 1: To install Java in Linux, first, let's install JRE from the Ubuntu repository. JRE will download Java, JVM, Java class library, and other tools present in JRE. To install JRE run the command sudo apt install default-jre. It will download and install the default version of JRE present in the repository of your Linux operating system. Step 1

Step 2: It will download some packages and ask for permission. Type Y for yes. Step 2

Step 3: This will install Java and JRE in your machine. To verify again, check the version using the command java -version. It will give Java version installed. Step 3

How to Install JDK in Linux?

Step 1: Now let's install JDK in Linux using the command sudo apt install default-jdk. It will download and install the default version of JRE present in the repository of your Linux operating system. Step 1

Note: This will install the default JDK i.e., OpenJDK.

Step 2: It will download some packages and ask for permission. Type Y for yes. Step 2

Step 3: This will install JDK on your machine. To verify again, check the version using the command javac -version. Step 3

It will give Java compiler version installed. This will confirm the successful installation of JDK as the compiler is part of JDK. JDK Install Confirmation

Step 4: You can have multiple Java installations like default JDK, Oracle JDK on one server. To configure which version is the default for the use, you can use the command update-alternatives. This command will configure and display all the java installations available on the server so that you can choose one of them. Step 4 As no other Java installation is available on the server, it will show that there is only one alternative.

Step 5:

  • In Linux operating systems, many programs are written in java using the JAVA_HOME variable to determine the java installation location.
  • The JAVA_HOME environment variable points to the directory where the Java runtime environment (JRE) is installed on your computer.
  • If JAVA_HOME variable is not set properly, we can't run java programs as OS will not be able to find at what location Java is installed on your machine.

Let's set the environment variable JAVA_HOME. First, open environment variables file using nano editor in Linux. Command: $ sudo nano /etc/environment Step 5

Step 6: Generally, default installation path for OpenJDK is as follows:/usr/lib/jvm/java-11-openjdk-amd64/bin/java. Copy the path which gives the location of Java installation as above and set the variable JAVA_HOME to the path as shown below. Step 6 Save the changes and exit the editor using Crtl+X.

Step 7: Now, the JAVA_HOME variable is set. To verify it check what's stored in the variable. Use source /etc/environment to go to the environment file. Step 7

Step 8: Check the path of JAVA_HOME variable using the command echo $JAVA_HOME. The echo command will print the path stored in the variable on the terminal. Step 8

It will show the path you just set as an output, which signifies that we have correctly set the environment variable. /usr/lib/jvm/java-11-openjdk-amd64/bin/java Path as Output

Your Linux machine is ready to develop and run Java applications.

Download and Install Java IDE on Linux.

  • Integrated development environment(IDE) is a software suite that comprises basic tools like editor, compiler, debugger to develop applications.
  • Java has various IDE's but mostly used and popular is Eclipse IDE.
  • Eclipse IDE is famous for Java Integrated Development Environment, but it has other IDE's like C/C++ IDE, JavaScript IDE, and PHP IDE. It can also be used for web development technologies. And it has all the necessary tools required for application development and testing.
  • One of the best perks of using Eclipse IDE is it has a user-friendly user interface(UI).

Here are the steps to install one of the famous IDE, Eclipse on the Linux Operating System : Step 1: Snap is an installer used for installing eclipse IDE. Install snap using the command : sudo apt install snap.

Step 1

Step 2: After installing a snap, install eclipse IDE using the command: sudo snap install --classic eclipse.

Step 2 It will start downloading eclipse IDE.

Step 3: After successful installation, you will see the following message. Step 3

Step 4: Search eclipse in the application search, and you will see the Eclipse IDE installed. Step 4

Write your first Java program on a Linux OS.

Let's write our first Java Program that prints Hello World!. Step 1: After launching the workspace, click on Create a new Java Project.

Step 1

Step 2: Select Java Project and Click on Next.

Step 2

Step 3: Give name for your project in Project name and click on Finish. Here project name is HelloWorld.

Step 3

Step 4: You will find a folder named by your project name, i.e., HelloWorld in the package explorer. Expand the folder and right-click on the src folder. Click on New and then on Package.

Step 4

Create a package in src folder as follows:

Create a package in src folder as follows

Step 5: Java is an Object Oriented Programming language which works using classes and objects. And a basic requirement to run any java program is that it must have at least one class. Create a new class in the package created above following the same steps.

Step 5

Create class as follows.

Create a class as follows

You can see the following window on eclipse. It has created all the classes and required methods. We have to write our executable statements in main method.

Classes and required Methods

Step 6: Write output statement to print HelloWorld!.

Step 6

Step 7: Run the code by clicking the Run icon in green color.

Step 7

Here is the output of HelloWorld.java program:

Here is the output of HelloWorld.java program

Conclusion

  • Java needs JDK that is superset of JRE and JVM to execute programs.
  • javac command is used to compile java code and javacommand is used to execute it.
  • java command activates JVM, which is responsible for executing java programs.
  • After successful installation, we need to set environment variables to execute java commands.
  • IDE consists of smart editor and other applications which are helpful to develop and debug Java applications easily.