scala-cli's JVM Install Location

HKT HKT
Views

scala-cli, one of the superpowers in the Scala ecosystem, is a versatile tool for running Scala scripts. It has a ton of options to play with. One of the arguments you can pass to scala-cli is to specify the JVM version to run a script under, which is as follows:

scala-cli --jvm 17 my-script.scala

When done so, scala-cli first looks for JDK installed in the system1, if not found, downloads the specified JVM using Coursier. It does not consider the JVM installed via package/version managers like sdkman.

$ java -version
openjdk version "17.0.13" 2024-10-15 LTS
OpenJDK Runtime Environment Zulu17.54+21-CA (build 17.0.13+11-LTS)
OpenJDK 64-Bit Server VM Zulu17.54+21-CA (build 17.0.13+11-LTS, mixed mode, sharing)

$ which java
/Users/***/.sdkman/candidates/java/current/bin/java

$ scala-cli --jvm 17 collect.scala
<download progress appears and disappears on download completion>
Downloading JVM zulu:17
<program output>

As you can see, scala-cli downloads the JDK 17 despite being installed (via sdkman). Now, the question is where does scala-cli install the downloaded JDK?

It does not install in the system path nor in any of the folders under the user’s home directory like ~/.cache or ~/.coursier. Instead, here is where it is downloaded and installed:

  • Downloaded to ~/Library/Caches/Coursier/v1/https/cdn.azul.com/zulu/bin/zulu17.56.15-ca-jdk17.0.14-macosx_aarch64.tar.gz
  • Installed2 to ~/Library/Caches/Coursier/arc/https/cdn.azul.com/zulu/bin/zulu17.56.15-ca-jdk17.0.14-macosx_aarch64.tar.gz/zulu17.56.15-ca-jdk17.0.14-macosx_aarch64

That I found out after a long haul of investigation. But isn’t there a programmatic way to find it out? There is.

$ scala-cli --jvm 17 -e 'println(sys.props("java.home"))'
/Users/****/Library/Caches/Coursier/arc/https/cdn.azul.com/zulu/bin/zulu17.56.15-ca-jdk17.0.14-macosx_aarch64.tar.gz/zulu17.56.15-ca-jdk17.0.14-macosx_aarch64/zulu-17.jdk/Contents/Home

Courtesy of Seth Tisue in the Scala Discord channel.

UPDATE: @Gedochao shared in the Scala Discord channel a straight forward alternative to find out the JDK installation path:

cs java-home --jvm 17

AFAIK, neither scala-cli nor coursier provides a way to manage the JVMs. So, if you want to uninstall the downloaded JVMs, you can do so manually by deleting the folders mentioned above.


  1. On MacOS, JDK is installed for the system under /Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home. Likewise, there are specific install paths for Linux and Windows. ↩︎

  2. On a MacOS/M1 system ↩︎

scala scala-cli coursier