Software Development

How to Install Java, OpenJDK on Ubuntu

Blog bannerBlog banner

Objective

Java is a versatile, object-oriented programming language developed by Sun Microsystems (acquired by Oracle Corporation). It is designed to be platform-independent, allowing developers to write code that can run on any device or platform with a Java Virtual Machine (JVM). Java is widely used for building a variety of applications, including web, mobile, enterprise, and embedded systems.

Java Development Kit (JDK) and Java Runtime Environment (JRE) are essential components of the Java platform, each serving a specific purpose in the development and execution of Java applications.

Java Development Kit (JDK)

The JDK is a software development kit that provides tools, executables, and binaries required for developing, compiling, and running Java applications. It includes the Java Compiler(javac), Java Virtual Machine (JVM), various development tools, and an extensive set of libraries and APIs.

Components:

Java Compiler (javac) : Translates Java source code into Java bytecode..

Java Virtual Machine (JVM) : Executes Java bytecode on the target hardware.

Development Tools : Includes tools for debugging (jdb), documentation generation (Javadoc), and packaging (jar).

Use Case :

Developers use the JDK during the software development phase to write, compile, and test Java applications. It provides everything needed for the entire development life cycle.

Java Runtime Environment (JRE)

The JRE is a subset of the JDK and provides the runtime environment for Java applications. It includes the JVM, class libraries, and other supporting files needed for the execution of Java applications.

Components:

Java Virtual Machine (JVM): Executes Java bytecode.

Class Libraries: A set of precompiled classes and methods that Java applications can use for common tasks.

Use Case :

End-users who only need to run Java applications use the JRE. Itdoesn't include development tools like the compiler or debugger.

Relationship between JDK and JRE:

 The JDK includes the JRE, meaning that if you install the JDK, you also have everything in the JRE. However, the reverse is not true—the JRE doesn't include development tools.

Developers use the JDK during the development phase to create and test applications. End-users only need the JRE to run applications.

In summary, the JDK is a comprehensive package for Java development, including tools and libraries for both development and execution, while the JRE is a runtime environment for end-users, containing only what is necessary to run Java applications.

Let’s Get Started

This guide is predicated on the assumption that you are using an Ubuntu 22.04 server. But don't worry if your server is running a different operating system, the commands and concepts are still mostly applicable.

Step 1 : let’s Install Java

1.1 update your apt package index:

To start with installation let's first update your apt package index in your Linux:

Code

    $ sudo apt update
            

Also, we need to check whether Java is already installed or not

Code

    $ java -version
            

Let’s install JRE from OpenJDK:

Code

    $ sudo apt install default-jre
            

Now you can verify the installation:

Code

    $ java -version
            

You'll get the output shown below:

Code

    $ sudo nano /etc/elasticsearch/elasticsearch.yml
            

By default, Elasticsearch listens on localhost and port 9200. Modify settings such as network.host and http.port if needed.

Initiate the Elasticsearch service using systemctl and allow a brief startup period to avoid connection errors:

java-installation

To compile and run any specialized Java-based software, you might also require the JDK in addition to the JRE. Use the following command to install the JDK, which will also install the JRE:

Code

    $ sudo apt install default-jdk
            

Jdk installation can be verified bychecking version of java compiler:

Code

    $ javac -version
            

You'll get the output shown below:

javac

Step 2: Setting up Multiple Java versions

You can run the command below to set up multiple Java installations on a server and set up different Java versions using the command line.

Code

    $ sudo update-alternatives --config java
            

Note: This command can also be used with the following other commands: key tool, java doc, and jarsigner, among others.

Step 3: Setting up JAVA_HOME Environment Variable

If you are running multiple versions use this command to find the path of the installation.

Code

    $ sudo update-alternatives --config java
            

If you are not running multiple versions use this command to find the path of installation.

Code

    $ sudo update-alternatives --display java
            

By running this command you will find output like this:

display-java

In my case java installation path is below : /usr/lib/jvm/java-11-openjdk-amd64

 Take a copy of the path from the installation you like. Following that, use nano or another text editor of your choice to open /etc/environment:

Code

    $ sudo nano /etc/environment
            

At the end of file you can add path as

Code

    $ JAVA_HOME=”PATH”
            

In this case it will be

Code

    $ JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
            

Save the file now, and then exit the editor.

We must execute the following command to make the modifications made in the current session visible:

Code

    $ source /etc/environment
            

To verify your changes run the following command :

Code

    $ echo $JAVA_HOME
            

Output : /usr/lib/jvm/java-11-openjdk-amd64

card user img
Twitter iconLinked icon

Zignuts Technolab delivers future-ready tech solutions and keeps you updated with the latest innovations through our blogs. Read, learn, and share!

Let’s Connect

Exclusive Free Consultation

Valid number
Claim My Spot!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
download ready
Thank You
Your submission has been received.
We will be in touch and contact you soon!

Our Latest Blogs

Load More

Our Latest Blogs

View All Blogs