The .java-version file
A .java-version file in the project root tells javm which JDK the project uses. Commit it with the code, and
everyone who builds the project, on any operating system and in CI, gets the same Java version.
Create it
The file holds a single selector:
echo 21 > .java-version
In PowerShell:
"21" | Out-File -Encoding ascii .java-version
Keep -Encoding ascii: Windows PowerShell 5.1 otherwise writes UTF-16, which javm and other tools cannot read.
Then install the JDK and run the build with it:
javm install
javm exec mvn verify
What it can contain
Any javm selector, on one line. Leading and trailing whitespace, including the final newline, is ignored.
| Content | Selects |
|---|---|
21 | The newest installed 21.x of any distribution, or the default distribution when installing |
21.0.5 | That exact version |
temurin@21 | The newest Temurin 21 |
~21.0.2 | >=21.0.2 <21.1.0 |
Java 8 and older use modern version numbers: write 8, not 1.8. The full syntax is in
Selectors and ranges.
For reproducible builds, use an exact version such as 21.0.5. A feature version such as 21 follows the newest
build installed on each machine.
Which commands read it
javm use, javm exec, javm install, and javm which read .java-version when you run them without a selector.
An explicit selector, such as javm use 17 or javm exec --jdk 17, takes precedence over the file.
javm uses the nearest .java-version in the current directory or its parents, so commands work from any
subdirectory of the project.
javm does not switch versions automatically when you change directories. Run javm use in the project, or run
commands through javm exec.
Share it with other tools
Other tools read .java-version too, but each understands a different syntax. When the file is shared, keep it to a
plain version number such as 21 or 21.0.5; the distribution@ prefix and ranges are javm-only.
| Tool | How it reads .java-version |
|---|---|
| jEnv | Reads and writes it with jEnv version names, such as 21.0.2 |
| mise | Reads it after mise settings add idiomatic_version_file_enable_tools java; a version without a vendor uses mise's default vendor, Temurin |
| setup-java (GitHub Actions) | Reads it with java-version-file: .java-version; the distribution input is still required |
To use the same file in CI without javm, point actions/setup-java at it. With javm in CI, see
Use javm in CI.