Java Development Environment
Install
Install Java using SdkMan. If you haven't installed SdkMan yet, install it first. (SdkMan Installation)
Available Java List
To get the list of available Java, you can run the following command.
sdk list java
Install Java 21
To install Java 21 Temurin (Adoptium),
sdk install java 21.0.5-tem
To install Liberica JDK from BellSoft,
sdk install java 21.0.5-librca
Install Java 17
To install Java 17 Temurin (Adoptium),
sdk install java 17.0.13-tem
To install Liberica JDK from BellSoft,
sdk install java 17.0.13-librca
Install Java 11
To install Java 11 Temurin (Adoptium),
sdk install java 11.0.25-tem
To install Liberica JDK from BellSoft,
sdk install java 11.0.25-librca
Install Java 8
To install Java 8 Zulu (No Temurin 8 available,
sdk install java 8.0.422-zulu
To install Liberica JDK from BellSoft,
sdk install java 8.0.422-librca
Create Symbolic Links
It's often convenient to create symbolic links to JDK.
The folders of the JDK installed by Coursier have the version info in it. So if you link them to IDE, you probably need to re-link the new folder whenever you upgrade JDKs. To avoid this hassle, you can use symbolic links to the JDKs instead of the actual folder paths.
jdk-sym-link
List Installed JDKs
To simplify symlink creation, you can use jdk-sym-link.
bash -c "$(curl -fsSL https://raw.githubusercontent.com/kevin-lee/jdk-sym-link/main/.scripts/install.sh)"
The one above should work for both Intel and M1 Macs but if it doesn't. try the JVM version below.
bash -c "$(curl -fsSL https://raw.githubusercontent.com/kevin-lee/jdk-sym-link/main/.scripts/install-jvm.sh)"
Once installation is done, run the following commands to see all installed JDKs.
jdkslink list
The expected result might be something like this.
$ ls -l /Library/Java/JavaVirtualMachines
total 0
drwxr-xr-x 4 root wheel 128 27 Sep 2021 adoptopenjdk-8.jdk
drwxr-xr-x 3 root wheel 96 25 Mar 2019 jdk1.8.0_202.jdk
lrwxr-xr-x@ 1 root wheel 53 14 Oct 22:50 jdk11 -> /Users/USERNAME/.sdkman/candidates/java/11.0.25-tem
lrwxr-xr-x@ 1 root wheel 50 15 Oct 02:17 jdk17 -> /Users/USERNAME/.sdkman/candidates/java/17.0.13-tem
lrwxr-xr-x@ 1 root wheel 52 14 Oct 22:50 jdk8 -> /Users/USERNAME/.sdkman/candidates/java/8.0.422-zulu
$ ls -l /Users/USERNAME/.sdkman/candidates/java
total 0
drwxr-xr-x@ 11 USERNAME staff 352 23 Aug 19:03 11.0.25-tem
drwxr-xr-x@ 11 USERNAME staff 352 19 Jul 00:41 17.0.13-tem
drwxr-xr-x@ 18 USERNAME staff 576 14 Jul 18:31 8.0.422-zulu
lrwxr-xr-x@ 1 USERNAME staff 10 14 Oct 19:27 current -> 17.0.13-tem
Symbolic Link
jdkslink slink 21
The expected result might be like
Version(s) found:
[0] 21.0.5-tem (21.0.2)
at /Users/USERNAME/.sdkman/candidates/java/21.0.5-tem
[c] Cancel
0
You chose '21.0.5-tem'.
It will create a symbolic link to '21.0.5-tem' (i.e. jdk21 -> 21.0.5-tem)
and may ask you to enter your password.
Would you like to proceed? (y / n)
y
/Library/Java/JavaVirtualMachines/jdk21: It is found so will be removed and recreated.
find /Library/Java/JavaVirtualMachines -type l -iname jdk21
/Library/Java/JavaVirtualMachines $ sudo rm jdk21
/Library/Java/JavaVirtualMachines $ sudo ln -s /Users/USERNAME/.sdkman/candidates/java/21.0.5-tem jdk21
Password:
Done!
# Before
--------------------------------------
total 0
drwxr-xr-x 4 root wheel 128 27 Sep 2021 adoptopenjdk-8.jdk
drwxr-xr-x 3 root wheel 96 25 Mar 2019 jdk1.8.0_202.jdk
lrwxr-xr-x@ 1 root wheel 51 11 Mar 01:40 jdk11 -> /Users/USERNAME/.sdkman/candidates/java/11.0.25-tem
lrwxr-xr-x@ 1 root wheel 51 11 Mar 01:40 jdk17 -> /Users/USERNAME/.sdkman/candidates/java/17.0.13-tem
lrwxr-xr-x@ 1 root wheel 52 11 Mar 01:40 jdk8 -> /Users/USERNAME/.sdkman/candidates/java/8.0.422-zulu
======================================
# After
--------------------------------------
total 0
drwxr-xr-x 4 root wheel 128 27 Sep 2021 adoptopenjdk-8.jdk
drwxr-xr-x 3 root wheel 96 25 Mar 2019 jdk1.8.0_202.jdk
lrwxr-xr-x@ 1 root wheel 51 11 Mar 01:40 jdk11 -> /Users/USERNAME/.sdkman/candidates/java/11.0.25-tem
lrwxr-xr-x@ 1 root wheel 51 11 Mar 01:40 jdk17 -> /Users/USERNAME/.sdkman/candidates/java/17.0.13-tem
lrwxr-xr-x@ 1 root wheel 50 23 Apr 21:48 jdk21 -> /Users/USERNAME/.sdkman/candidates/java/21.0.5-tem
lrwxr-xr-x@ 1 root wheel 52 11 Mar 01:40 jdk8 -> /Users/USERNAME/.sdkman/candidates/java/8.0.422-zulu
======================================
Now, let's create the symbolic link for Java 11 as well,
jdkslink slink 11
Set Default and Local Java Version
Set Default Java Version
To set the default Java version to 17,
sdk default java 21.0.5-tem
To check,
sdk current
Using:
java: 21.0.5-tem
Set Project-specific Java Version
Make sure sdkman_auto_env
is set to true
.
If you haven't set it up or not sure what it is, check out the SdkMan Setup.
Move to the project folder where you want to set the project-specific Java version.
sdk env init
.sdkmanrc created.
Check the current Java version
sdk current
Using:
java: 17.0.13-tem
Let's check out the content of the file.
cat .sdkmanrc
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=17.0.13-tem
Edit the java in .sdkmanrc
to change the Java version for the project.
e.g.)
java=11.0.25-tem
Check the current Java version
sdk current
Using:
java: 11.0.25-tem