Manually Install JDK on Linux

This article applies to:

  • Platform(s): Xubuntu Linux
  • Java version: 22.0.2

Download

  1. Go to https://jdk.java.net and download the JDK version you want. For now, I choose JDK 22, and pick the download link for Linux/64 build, tar.gz format of JDK 22.0.2 General-Availability Release. So my downloaded file is openjdk-22.0.2_linux-x64_bin.tar.gz.
  2. Check the download file size and sha256 hash string to make sure that you have downloaded the full, uncorrupted file. For example, the jdk.java.net site reports that the Linux/x64 build file is 201996264 bytes, and the SHA256 hash string is 41536f115668308ecf4eba92aaf6acaeb0936225828b741efd83b6173ba82963
    xubuntu:~/share/download/java$ ls -l
    total 197264
    -rwxrwxrwx 1 root root 201996264 Aug 26 14:45 openjdk-22.0.2_linux-x64_bin.tar.gz
    xubuntu:~/share/download/java$ sha256sum openjdk-22.0.2_linux-x64_bin.tar.gz 
    41536f115668308ecf4eba92aaf6acaeb0936225828b741efd83b6173ba82963 openjdk-22.0.2_linux-x64_bin.tar.gz
    

Install

  1. I like to extract the JDK content to a temporary folder before moving/copying it to the default folder of /usr/lib/jvm.
    xubuntu:~/share/download/java$ mkdir ~/temp/jdk
    xubuntu:~/share/download/java$ tar -xvzf openjdk-22.0.2_linux-x64_bin.tar.gz -C ~/temp/jdk
    jdk-22.0.2/bin/jar
    jdk-22.0.2/bin/jarsigner
    jdk-22.0.2/bin/java
    jdk-22.0.2/bin/javac
    ... omitted-output ...
    jdk-22.0.2/lib/src.zip
    jdk-22.0.2/lib/tzdb.dat
    jdk-22.0.2/release
    xubuntu:~/share/download/java$ cd ~/temp/jdk
    xubuntu:~/temp/jdk$ ls -l
    total 4
    drwxrwxr-x 8 h0ward h0ward 4096 Aug 26 15:18 jdk-22.0.2
    
  2. Move the new JDK content to /usr/lib/jvm
    xubuntu:~/temp/jdk$ sudo mkdir /usr/lib/jvm
    xubuntu:~/temp/jdk$ sudo mv jdk-22.0.2/ /usr/lib/jvm
    xubuntu:~/temp/jdk$ ls -l /usr/lib/jvm
    total 4
    drwxrwxr-x 8 h0ward h0ward 4096 Aug 26 15:18 jdk-22.0.2
    

Configure

  1. Update environment variables to point to the newly installed JDK by editing the /etc/environment file.
    xubuntu:~/temp/jdk$ sudo nano /etc/environment
    
  2. Add JAVA_HOME variable, and JDK bin to PATH.
    JAVA_HOME="/usr/lib/jvm/jdk-22.0.2"
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/lib/jvm/jdk-22.0.2/bin"
    

Install Oracle JDK 10.0.1 on Linux

[Updated for jdk-10.0.1]
This is for 64-bit installation.

  1. Download JDK from Oracle site
    http://www.oracle.com/technetwork/java/javase/downloads
    The installation file I use is jdk-10.0.1_linux-x64_bin.tar.gz.
  2. Change directory to install the JDK code in /opt
    cd /opt
  3. extract the JDK files
    sudo tar -xvzf <location of downloaded JDK>/jdk-10.0.1_linux-x64_bin.tar.gz 

    This will put the JDK into the newly created directory named jdk-10.0.1

  4. Update environment variables to point to the newly installed JDK by editing the /etc/environment file.
    sudo nano /etc/environment

    Add the JAVA_HOME variable, and add JDK bin to the PATH variable.

    JAVA_HOME="/opt/jdk-10.0.1"
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/jdk-10.0.1/bin" 
  5. Refresh the environment variable.
    source /etc/environment
  6. Verify that the updated environment variables are in place and the path to the JDK is valid
    $ echo $JAVA_HOME
    /opt/jdk-10.0.1
    $ echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/jdk-10.0.1/bin
    
  7. Update Java alternative list. You want to set a higher priority for the new JDK. On my system this is the result when I query the alternatives:
    $ update-alternatives --verbose --query java
    Name: java
    Link: /usr/bin/java
    Slaves:
     java.1.gz /usr/share/man/man1/java.1.gz
    Status: auto
    Best: /usr/lib/jvm/java-9-openjdk-amd64/bin/java
    Value: /usr/lib/jvm/java-9-openjdk-amd64/bin/java
    
    Alternative: /usr/lib/jvm/java-9-openjdk-amd64/bin/java
    Priority: 1091
    Slaves:
     java.1.gz /usr/lib/jvm/java-9-openjdk-amd64/man/man1/java.1.gz
    

    So, let’s make the new JDK our default…

    sudo update-alternatives --install /usr/bin/java java /opt/jdk-10.0.1/bin/java 10001

    The response is:

    update-alternatives: using /opt/jdk-10.0.1/bin/java to provide /usr/bin/java (java) in auto mode.

    Now, when you type java -version, you will see

    java version "1.0.1" 2018-04-17
    Java(TM) SE Runtime Environment 18.3 (build 1.0.0.1+10)
    Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
  8. Likewise, set the new Java compiler as default with

    $ sudo update-alternatives --install /usr/bin/javac javac /opt/jdk-10.0.1/bin/javac 10001
    update-alternatives: using /opt/jdk-10.0.1/bin/java to provide /usr/bin/javac (javac) in auto mode
    $ javac -version
    java version "10.0.1" 2018-04-17
    Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
    Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
  9. If you wish, you can remove the link to the older java alternative
    $ sudo update-alternatives --verbose --remove java /usr/lib/jvm/java-9-openjdk-amd64/bin/java
    update-alternatives: discarding obsolete slave link java.1.gz (/usr/share/man/man1/java.1.gz)
    $ update-alternatives --verbose --query java
    Name: java
    Link: /usr/bin/java
    Status: auto
    Best: /opt/jdk-10.0.1/bin/java
    Value: /opt/jdk-10.0.1/bin/java
    
    Alternative: /opt/jdk-10.0.1/bin/java
    Priority: 10001
    
  10. In the similar way, remove the old javac alternative link.
    $ update-alternatives --verbose --query javac
    Name: javac
    Link: /usr/bin/javac
    Slaves:
     javac.1.gz /usr/share/man/man1/javac.1.gz
    Status: auto
    Best: /opt/jdk-10.0.1/bin/java
    Value: /opt/jdk-10.0.1/bin/java
    
    Alternative: /opt/jdk-10.0.1/bin/java
    Priority: 10001
    Slaves:
    
    Alternative: /usr/lib/jvm/java-9-openjdk-amd64/bin/javac
    Priority: 1091
    Slaves:
     javac.1.gz /usr/lib/jvm/java-9-openjdk-amd64/man/man1/javac.1.gz
    $ sudo update-alternatives --remove javac /usr/lib/jvm/java-9-openjdk-amd64/bin/javac
    

Java Primitive Data Types

Type Length Range
byte 8-bit signed two’s complement integer. minimum: -128; maximum: 127 (inclusive)
short 16-bit signed two’s complement integer. minimum: -32,768; maximum: 127 (inclusive)
int 32-bit signed two’s complement integer.
in Java 8 and later: int can represent an unsigned 32-bit integer.
signed — minimum: -231; maximum: 231-1 (inclusive)
unsigned — minimum: 0; maximum: 232-1
long 64-bit signed two’s complement integer.
in Java 8 and later: int can represent an unsigned 64-bit integer.
signed — minimum: -263; maximum: 263-1 (inclusive)
unsigned — minimum: 0; maximum: 264-1
float single-precision 32-bit IEEE 754 floating point. See Java Language Specification. Don’t use for precise values, such as currency.
double single-precision 64-bit IEEE 754 floating point. See Java Language Specification. Don’t use for precise values, such as currency.
boolean not precisely defined true and false
char single 16-bit Unicode character minimum: '\u0000' (0); maximum: '\uffff' (65,535) inclusive
Data Type Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
boolean false
char '\u0000'
String (or any object) null

Java Language Keywords — A’s (2)

Keywords: abstract, assert

abstract

As a class modifier, abstract is a nonaccess modifier that declares a class which cannot be instantiated. You cannot create an object from abstract classes. However, you can create subclasses from abstract classes by extending them. An abstract class may or may not include abstract methods. If a class includes abstract methods, the class itself must be declared abstract. When a subclass does not provide all of the abstract methods in its parent class, the subclass must also be declared abstract.

As a method modifier, abstract declares a method that must be implemented by a subclass. The first concrete (not abstract) subclass must implement all abstract methods of the superclass. Abstract methods cannot have the modifiers final or private because subclasses need to access the methods in order to implement them.

It is a compile-time error if a abstract method declaration also contains any one of the keywords private, static, final, native, strictfp, or synchronized.

An abstract method is declared without an implementation. In other words, without braces, and followed by a semicolon, as in the following example code:

public abstract class Shape {
  int x, y;
  ...
  void moveTo(int x, int y) {
    ...
  } // moveTo(int, int)

  abstract void draw();
  abstract void scale(int factor);
} // class Shape

Then each concrete subclass of Shape, such as Circle and Triangle, must implement the draw and scale methods:

class Circle extends Shape {
  void draw() {
    ...
  } // draw()

  void scale(int factor) {
    ...
  } // scale(int)
} // class Circle

class Triangle extends Shape {
  void draw() {
    ...
  } // draw()

  void scale(int factor) {
    ...
  } // scale(int)
} // class Triangle

Abstract class provide default functionality for the subclasses, whereas an interface mandates a list of functions or properties. So, objects that extends an abstract class “Is-A” subclass; objects that implement an interface “Must-Do” the functions, or “Must-Have” the constants or nested types of the interface.

If an abstract class contains only abstract method declarations, it should be declared as an interface instead. Still, keep in mind that when you make a change to an abstract class all of the subclasses will now have this new functionality. On the other hand, once an interface is changed, any class that implements it that will be broken.

assert
[awaiting definition note]

[deprecated] Using Oracle’s plugins in Firefox

Deprecated post: This post is left here for archival purpose. Beginning in Firefox version 52 released March 7, 2017, installed NPAPI plugins are no longer supported in Firefox.

Enable Oracle’s JRE plugin for Firefox in Linux.

  1. See my other post on Installing Oracle JDK 10.0.1 on Linux.
  2. Create your .mozilla/plugins directory if one isn’t already exists.
    mkdir ~/.mozilla/plugins
  3. Create a symbolic link of the JRE’s libnpjp2.so shared object file in your plugins directory. In my installation, this JRE file is found in /opt/jdk-10.0.1/lib/libnpjp2.so
    ln -s /opt/jdk-10.0.1/lib/libnpjp2.so ~/.mozilla/plugins 
  4. Restart Firefox and browse to one of the JVM tester page to see the version of Java your browser is using.