Skip to main content

Switch Java versions

Choose the right command​

I want to...Use
Run one command with a JDKjavm exec [--jdk <selector>] <command> (see Run commands with a JDK)
Switch the JDK in the current shelljavm use <selector>
Choose a JDK for a projectAdd .java-version, then run javm use or javm exec in that directory
Set the JDK used in new shellsjavm default <selector>
Give a selector a short namejavm alias <name> <selector>

javm use and javm deactivate change the current shell, so they need the shell integration. The other commands work without it.

Switch the current shell​

javm use temurin@21
java --version
javm current # prints the JDK javm activated in this shell
javm deactivate # restores the previous PATH and JAVA_HOME

A selector can be a version or a range, such as javm use 21 or javm use zulu@~8.0.400 (>=8.0.400 <8.1.0). See Selectors and ranges.

javm deactivate removes javm's entries from PATH and restores the previous JAVA_HOME. It does not remove the JDK; use javm uninstall for a managed JDK or javm unlink for a linked one.

Pick a JDK per project​

Create a .java-version file in the project root with a selector, for example:

21

Then, in that directory:

javm install # installs a matching JDK if needed
javm use # switches the current shell to it

An explicit selector, such as javm use 17, takes precedence over .java-version. javm does not switch automatically when you change directories, and javm use without a selector does not fall back to the default.

Set a default for new shells​

javm default temurin@21

The selector is saved in default-version in JAVM_HOME, and the shell integration activates it whenever a new shell starts.

Name a selector with an alias​

javm alias project temurin@21 # create or update
javm alias project # show the selector
javm use project # use it like any selector
javm unalias project # delete

use and which accept an alias name as a selector. Updating an alias points it to the best matching JDK.

Find the path of a JDK​

javm which temurin@21
javm which # uses .java-version
javm which --home 21 # on macOS, appends /Contents/Home for JAVA_HOME