Skip to content
Latchkey

Wrong Java Version on Runner - Pin the JDK in CI

The runner has several JDKs installed and the build picked the wrong one. JAVA_HOME or PATH resolves to an unintended JDK, so compile/runtime behavior differs from what you expect.

What this error means

The build compiles or runs against an unexpected Java version - a version-dependent test fails, or java -version in the log shows a JDK you did not intend, even though setup ran.

shell
+ java -version
openjdk version "11.0.22" 2024-01-16
# expected 21 - JAVA_HOME points at the preinstalled JDK 11, not the one set up

Common causes

JAVA_HOME points at a preinstalled JDK

The image ships multiple JDKs; JAVA_HOME (or a PATH entry) resolves to a preinstalled version rather than the one you configured.

setup-java step ordered after the build

If the JDK-selection step runs after (or in a different job from) the build, the build uses whatever default JDK was active.

How to fix it

Pin the JDK and assert it before building

Select the JDK with setup-java early, then assert the version so a mismatch fails loudly.

.github/workflows/ci.yml
- uses: actions/setup-java@v4
  with:
    distribution: temurin
    java-version: '21'
- run: |
    java -version
    test "$(java -version 2>&1 | grep -c '\"21')" -ge 1

Make the build select the JDK via toolchain

Decouple the build from the ambient JDK by pinning a toolchain in the build tool.

build.gradle.kts
// build.gradle.kts
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }

How to prevent it

  • Pin the JDK with setup-java (or a toolchain), assert java -version early, and avoid relying on the runner default JDK.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →