How to install Java

Java
Published on: Jan 31, 2024
  1. install SDKMAN!

  2. Terminal window
    sdk install java 21.ea.35-open

    Installs Java 21.

  3. Terminal window
    java --version

    Check Java installation.

Switching to a specific Java version in different projects

Suppose you have multiple projects that require different Java versions.

Project 1 uses Java 21.ea.35.

Project 2 uses Java 22.ea.29.

- project-1/
- ...
- project-2/
- ...
  1. Add .sdkmanrc files with the required Java versions to the project directories.

    project-1/.sdkmanrc
    java=21.ea.35-open
    project-2/.sdkmanrc
    java=22.ea.29-open
    - project-1/
    - .sdkmanrc
    - project-2/
    - .sdkmanrc
  2. Enable switching SDK versions automatically when you cd into a directory

    ~/.sdkman/etc/config
    ...
    sdkman_auto_env=true
    ...

After setting up the .sdkmanrc files, SDKMAN! will automatically switch to the required Java version when you cd into the project directories.

If specified Java version is not installed, SDKMAN! will prompt you to install it.

Run sdk env install to install it.