• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Java | Install Java (Ubuntu)
  1. Install Java
  2. Manage multiple Java versions (update-alternatives)
    1. Check defaults
    2. Add a new java alternative to the system
    3. Configure java alternatives
    4. Set JAVA_HOME

  1. Install Java
    Download the JDK:
    Oracle: https://www.oracle.com/java/technologies/downloads/
    OpenJDK:https://openjdk.org/

    Extract the file "jdk-23_linux-x64_bin.tar.gz" to the installation directory: /opt/jdk-23.0.2
    $ sudo tar -xf jdk-23_linux-x64_bin.tar.gz -C /opt/
    Change the folder permissions (replace 'mtitek' with your username):
    $ sudo chmod -R 755 /opt/jdk-23.0.2
    $ sudo chown -R mtitek:mtitek /opt/jdk-23.0.2
    Setup the environment variables JAVA_HOME and PATH:
    • Open the file "~/.profile" or "~/.bashrc" for persistent settings:
      $ vi ~/.profile
    • Append the following lines:
      export JAVA_HOME=/opt/jdk-23.0.2
      export PATH=$JAVA_HOME/bin:$PATH
    • Reload the configuration file:
      $ source ~/.profile
    Verify the installation and environment variables:
    $ java -version
    openjdk version "23.0.2" 2025-01-21
    OpenJDK Runtime Environment (build 23.0.2+7-58)
    OpenJDK 64-Bit Server VM (build 23.0.2+7-58, mixed mode, sharing)
    Locate the java executable:
    $ which java
    /opt/jdk-23.0.2/bin/java
  2. Manage multiple Java versions (update-alternatives)
    The update-alternatives system allows you to manage multiple Java installations and switch between them easily.

    1. Check defaults
      Check if alternatives are already configured:
      $ ls -al /usr/bin/java
      lrwxrwxrwx 1 root root /usr/bin/java -> /etc/alternatives/java
      $ ls -al /etc/alternatives/java
      lrwxrwxrwx 1 root root /etc/alternatives/java -> /usr/lib/jvm/java-17-openjdk-amd64/bin/java
      $ update-alternatives --list java
    2. Add a new java alternative to the system
      Register your Java installation with update-alternatives (adjust paths as needed):
      $ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk-23.0.2/bin/java" 1
      $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk-23.0.2/bin/javac" 1
      Display current alternatives:
      $ update-alternatives --display java
      java - auto mode
        link currently points to /usr/lib/jvm/java-17-openjdk-amd64/bin/java
      
      /opt/jdk-23.0.2/bin/java - priority 1
      /usr/lib/jvm/java-17-openjdk-amd64/bin/java - priority 1711
      
      Current 'best' version is '/usr/lib/jvm/java-17-openjdk-amd64/bin/java'.
    3. Configure java alternatives
      Select which Java version to use as default:
      $ sudo update-alternatives --config java
      There are 2 choices for the alternative java (providing /usr/bin/java).
      
        Selection    Path                                        Priority   Status
      ------------------------------------------------------------
      * 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java  1711      auto mode
        1            /opt/jdk-23.0.2/bin/java                     1         manual mode
        2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java  1711      manual mode
      
      Press <enter> to keep the current choice[*], or type selection number: 1
      update-alternatives: using /opt/jdk-23.0.2/bin/java to provide /usr/bin/java (java) in manual mode
      Also configure javac if you installed the compiler:
      $ sudo update-alternatives --config javac
    4. Set JAVA_HOME dynamically
      Set JAVA_HOME to automatically point to the currently selected Java version:
      export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
      Or add this to your shell profile for persistence:
      $ echo 'export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")' >> ~/.profile
© 2025  mtitek