Thursday, August 11, 2016

How to Install Java In Ubuntu

Which Version of Java to Use?

  • JDK 7 supports:
    • eclipse version 4.4 - 4.5
    • JBoss AS 7 with Java EE 6 features
    • Tomcat 7-8 with WebSockets
  • JDK 8 (LTS) supports:
    • eclipse from eclipse Neon (4.6) to eclipse 2020-06 (4.16)
    • WildFly from 8 to 13 with Java EE 7 features
    • WildFly from 14 to 25 with Jakarta EE 8 features
    • Tomcat 9 with Java EE 8 features
    • Eclipse GlassFish 5.1.0 with Java EE 8 and Jakarta EE 8
    • Eclipse Glassfish 6.0.0 with Jakarta EE 9
  • JDK 11 (LTS) supports:
    • eclipse IDE from Eclipse 4.17 (2020-09) to Eclipse 4.24 (2022-06)
    • Eclipse Glassfish 6.1 with Jakarta EE 9.1
  • JDK 17 (LTS) supports:
    • eclipse IDE from eclipse 4.25 (2022-09) to eclipse 4.28 (2023-06)
    • Eclipse Glassfish 6.2.2 with Jakarta EE 9.1

By march 2019, Java SE 8 will go through the End of Public Updates process, because it is a legacy release.

The java programming language is an open source project. The OpenJDK community works for a free open-source implementation of the Java SE standard and Oracle contribute to the OpenJDK project. Beginning with Java SE 11 (September 2018, LTS), Oracle provide free releases and commercially supported releases for use with Oracle products: OpenJDK (free) and Oracle JDK (commercial) builds from Oracle. The OpenJDK project is the basis for both OpenJDK and Oracle JDK builds: applications will run interchangeably on Oracle JDK-11 and OpenJDK JDK-11.

Searching the OpenJDK package in the official repo

Ubuntu users can install OpenJDK by using an official Ubuntu repository.

Check out which versions are present in the official Ubuntu repository

$ apt-cache search openjdk

Check out the description of the openjdk-8-jdk package:

$ apt show openjdk-8-jdk
Package: openjdk-8-jdk
Version: 8u292-b10-0ubuntu1~20.04
Priority: optional
Section: universe/java
Source: openjdk-8
Origin: Ubuntu
Maintainer: Ubuntu Developers 
Original-Maintainer: OpenJDK Team 
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 3.288 kB
Provides: java-compiler, java-sdk, java2-sdk, java5-sdk, java6-sdk, java7-sdk, java8-sdk
Depends: openjdk-8-jre, openjdk-8-jdk-headless, libc6
Recommends: libxt-dev
Suggests: openjdk-8-demo, openjdk-8-source, visualvm
...

OpenJDK Java SE Development Kit 21 (JDK-21) Installation

To install the OpenJDK JDK-21, issue the command:

sudo apt install openjdk-21-jdk

To install the OpenJDK JDK-21, with source, demo, JVM profiling tools and documentation, issue the command:

sudo apt install openjdk-21-jdk openjdk-21-source openjdk-21-doc openjdk-21-demo visualvm

When you install OpenJDK using apt, the JDK is usually installed under:

/usr/lib/jvm/

Each JDK version will have its own subfolder, for example:

  • /usr/lib/jvm/java-21-openjdk-amd64/
  • /usr/lib/jvm/java-17-openjdk-amd64/
  • /usr/lib/jvm/java-11-openjdk-amd64/

You can check which exact version you installed and its path with:

$ update-alternatives --config java

This will list all installed Java versions and their installation directories.

Or to find the current java binary:

$ which java

Then trace its path to the JDK, using the readLink command:

$ readlink -f $(which java)
/usr/lib/jvm/java-21-openjdk-amd64/bin/java

Multiple JDKs Installation

sudo apt install openjdk-21-jdk openjdk-8-jdk
# or
sudo apt install --install-suggests openjdk-21-jdk openjdk-8-jdk

Show all available JDK with update-alternatives --list command:

$ sudo update-alternatives --list java
/usr/lib/jvm/java-21-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Set the default JDK with update-alternatives --config command:

$ sudo update-alternatives --config java
[sudo] password for user: 
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press enter to keep the current choice[*], or type selection number: 

Oracle Java SE Development Installation via APT PPA Repository

To install Oracle's Java, using PPA personal repositories, see here.

Do I Have to Set the PATH environment variable ?

It is not required to set the PATH variable to add the Java bin directory to the system path.

$ java -version
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

Setting the JAVA_HOME environment variable

The JAVA_HOME variable is used by Java Servers, such as Tomcat and JBoss, and by java tools, such as ant and maven.

The JAVA_HOME variable should point to the installation folder of the JDK.

Note that if you run maven from command line, you need to specify the JAVA_HOME, whereas, if you run maven from your IDE, you are not required to indicate the JAVA_HOME, as you have already configured the path of the installed JRE inside your IDE.

No comments:

Post a Comment