1. What is JAVA?

1.1. Introduction

Java, introduced in 1995, is a globally renowned programming language originally developed by Sun Microsystems, now owned by Oracle Corporation. It is designed for building robust, scalable applications that run on various platforms, such as Windows, macOS, Linux, and mobile devices.

1.2. Java Key Features:

  • Platform Independence
  • Object-Oriented Programming (OOP)
  • Robustness
  • High Security
  • Multithreading
  • Performance

1.3. Use Cases

  • Building Desktop Application
  • Building Web Applications
  • Developing Microservices
  • Big Data
  • Building Mobile Apps
  • Gaming Platforms

2. Installing Java:

2.1. Installing Java on Ubuntu:

2.1.1. Install OpenJDK Java on Ubuntu

Step 1: Update Ubuntu Repositories

sudo apt update

Next, check if Java is already installed:

java -version

Step 2: Search OpenJDK using Apt Cache

sudo apt-cache search openjdk

Step 3: Install specific version of OpenJDK Java on Ubuntu

sudo apt install openjdk-#-jdk

Replace # with your preferred version number, for example

sudo apt install openjdk-17-jdk

Now, press ‘y’ and ‘Enter’ to proceed with the installation.

Step 4: Verify Java Installation

java -version

Check the Java compiler version to verify that the full JDK platform is installed on your system:

javac -version
2.1.2. Install Oracle JDK Java on Ubuntu

You can install Oracle JDK, which offers Oracle’s Java implementation as an alternative to OpenJDK. The OpenJDK is an open-source implementation of Java, is readily available and comes bundled with Ubuntu. The Oracle JDK, which is the licensed Java version maintained exclusively by Oracle, the developers of Java.

Step 1: Oracle’s Java licensing restricts automatic installation via package managers. To install Oracle JDK, you must manually download it from Oracle Archive Downloads., based on your OS architecture type.

💡
Not sure about your OS architecture!, then type uname -m in terminal.
If it’s x86_64 indicates an Intel or AMD 64-bit processor aarch64 or arm64 denotes an ARM64 architecture.

Copy the link from the page, and use below command to download.

wget https://download.oracle.com/java/17/archive/jdk-17.0.6_linux-aarch64_bin.tar.gz

💪
Sometimes, Oracle requires login or license acceptance. In such cases, you must download the JDK from browser and move it to installation server.

Step 2: Follow bunch of commands to extract and configure java manually.

#create jdk directory
sudo mkdir /opt/oracle-jdk

#uncompress, change to your file name
sudo tar -zxf jdk-17.0.6_linux-aarch64_bin.tar.gz -C /opt/oracle-jdk

#check if files are there
ls /opt/jdk

#update alternatives so the command java point to the new jdk 
sudo update-alternatives --install /usr/bin/java java /opt/oracle-jdk/jdk-17.0.6/bin/java 100


#update alternatives so the command javac point to the new jdk 
sudo update-alternatives --install /usr/bin/javac javac /opt/oracle-jdk/jdk-17.0.6/bin/javac 100

#check if java command is pointing to " link currently points to /opt/oracle-jdk/jdk-17.0.6/bin/java"
sudo update-alternatives --display java

#check if java command is pointing to " link currently points to /opt/oracle-jdk/jdk-17.0.6/bin/javac"
sudo update-alternatives --display javac

#If your java version doesn't change (show and choose from list alternatives)
sudo update-alternatives --config java

#check if java is running
java -version

2.2. Installing Java on Mac

2.2.1. Install OracleJDK Java on Mac

We can install java in 2 ways, like as below.

2.2.1.1 Download DMG Installer JDK from Oracle Website

Step 1: Download DMG installer from Oracle JDK Website

Step 2: Open the downloaded JDK DMG file by double-clicking on it. It will initiate the process of installing Oracle 17 JDK (might be yours different jdk version).

Step 3: Click on “Continue” button and follow the GUI steps process until “Install Succeeded”.

2.2.1.2 Install through terminal via homebrew
💡
Homebrew is the package manager for MAC which makes installing software easy!
brew cask install java

The brew command install JDK, then after installation success you will seen the below message on the screen. java was successfully installed!

It’s time to verify the java version

java -version
2.2.2. Install OpenJDK Java on Mac

OpenJDK also we can install in 2 ways as shown below.

2.2.2.1 Download Manually from OpenJDK Archive Website

Step 1: Download your respective version from the OpenJDK Archive Website

Step 2: Now we can extract the downloaded binary file by navigating to the current directory in the terminal and executing the below command.

tar -xf openjdk-17.0.2_macos-aarch64_bin.tar.gz

Step 3: Add java path to your PATH using below command.

export  PATH=$PWD/jdk-17.0.2.jdk/Contents/Home/bin:$PATH
2.2.2.2 Download OpenJDK By using Homebrew

We can install most recent stable release version of OpenJDK by using the Homebrew.

brew install openjdk
2.2.3. Set/Change the JAVA_HOME Path on Mac

Step 1: Determine your terminal type by running the command: echo $SHELL. Based on the output, such as bash or zsh, update the Java path in the corresponding profile file.

Step 2: Find the java installation folder by using below command.

/usr/libexec/java_home

## If you want to see all installed version in Mac
/usr/libexec/java_home -V

Step 3: On MacOS 10.15 Catalina and later, the zsh is the default Terminal shell, so we have to set the $JAVA_HOME environment variable in either ~/.zshenv or ~/.zshrc file.

Open in any text editor or terminal to set JAVA_HOME path in nano nano ~/.zshenv as shown below

## Add following command to add java_home 
export JAVA_HOME=$(/usr/libexec/java_home)

## OR

## Use below command to set specific version of java
export JAVA_HOME=$(/usr/libexec/java_home -v17)

Step 4: For other older Mac OS X, the bash is the default Terminal shell, and we can set the $JAVA_HOME environment variable in either is older version ~/.bash_profile or ~/.bashrc

Open in any text editor or terminal to set $JAVA_HOME path in nano nano ~/.bash_profile as shown below.

## Add following command to add java_home 
export JAVA_HOME=$(/usr/libexec/java_home)

## OR

## Use below command to set specific version of java
export JAVA_HOME=$(/usr/libexec/java_home -v17)

3.3. Installing Java on Windows

Installing java in windows is easy peasy

Step 1: Download the installer file form the Oracle Website then double click on the installation file then follow the installation GUI steps.

Step 2: Set environment variable with below steps

  • Search Bar or in System Settings find the advanced system settings
  • System Properties > Advanced tab > Environment Variables
  • In the System Variables section, click New or Edit
  • In the Variable Name field, enter JAVA_HOME
  • In the Variable Value filed, enter the path to your JDK installation root folder.
    E.g., C:\Program Files\Java\jdk*x.x.x
  • select the PATH variable, under the System Variables section click Edit and add your JAVA_HOME path
    E.g., %JAVA_HOME%\bin

Conclusion:

By following the steps outlined in this guide, you can successfully install Java and configure the JAVA_HOME environment variable on Linux, macOS, and Windows systems. Ensuring the correct version of Java is installed and JAVA_HOME is properly set is crucial for seamless development and execution of Java applications.

Categorized in:

Java, Tech,

Tagged in:

, , , , ,