{
  "id": 22727935,
  "title": "Switch Java Versions per Project: update-alternatives & JAVA_HOME",
  "url": "https://urgent.news/2026/11/30/switch-java-versions-per-project-update-alternatives-java-home",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-30T04:46:58.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/nora-whitfield/switch-java-versions-per-project-update-alternatives-javahome-1c2f"
  },
  "original_language": null,
  "account": "Java 21 and Java 25 have been available as long-term support releases since September 2023 and September 2025 respectively. Many developers now find themselves with both versions installed on a single machine, often when working on different projects requiring distinct Java versions. However, managing system-wide defaults and per-project setups can be cumbersome, as switching the system default each time you switch between repositories can become tedious, and discrepancies between `java -version` and your build tool remain unresolved.\n\nThis tutorial presents a three-tiered approach to managing multiple Java versions: system default, shell or directory override, and per-build pinning. The tutorial then details a step-by-step checklist to troubleshoot the common issue where Java 25 is installed, but Maven continues to compile with Java 21.\n\nFirst, install both Java 21 and Java 25 side by side using Ubuntu's package archive. On Ubuntu 24.04, `openjdk-21-jdk` is the default JDK, while `openjdk-25-jdk` is available in the Universe repository. Enable Universe and update the package index before installing the JDK packages. The JDK packages include both the runtime and development tools, so a separate JRE is unnecessary if you only need to run existing Java applications.\n\nNext, understand the three levels that determine the Java version used by your system:\n\n1. System-wide default using `update-alternatives`: This method uses Debian's alternatives system to manage multiple Java installations. The `/usr/bin/java` and `/usr/bin/javac` executables are symbolic links to different JDK installations, controlled by `update-alternatives`. You can set the system default by choosing an entry with `update-alternatives --config java` and `update-alternatives --config javac`. However, this method may not be ideal for developers switching between projects frequently.\n\n2. Per-shell or per-directory override using `JAVA_HOME`: Setting the `JAVA_HOME` environment variable to the desired JDK's `bin` directory allows the JDK to be used by newly launched tools. You can create a shell function, `use-java`, to quickly switch between Java versions, and use `direnv` to automatically apply the correct Java version within a directory. This method is more flexible for developers who work on different projects requiring different Java versions.\n\n3. Per-build pinning using Maven toolchains or Gradle toolchains and the daemon JVM: This method involves specifying the JDK version to use for each build, regardless of the shell or directory settings. Maven's `toolchains` feature or Gradle's `toolchains` plugin allows you to pin the JDK version used for compilation, ensuring consistency across builds.\n\nWhen diagnosing a version mismatch, follow this order:\n\n1. Check the system-wide default by running `java -version` and `javac -version`.\n2. Verify the `JAVA_HOME` environment variable and the corresponding `PATH` configuration.\n3. Finally, inspect the build tool's toolchain configuration to ensure the correct JDK version is being used.\n\nWith this structured approach, developers can effectively manage multiple Java versions, ensuring compatibility across their projects without the hassle of constantly switching system defaults.",
  "summary": "The JDK 21 release page lists general availability on 19 September 2023, and JDK 25 followed on 16 September 2025; both are long-term support releases from most vendors. Since then, many developers have ended up with both versions on one laptop: an older service that must stay on 21, and a newer project that wants 25. The trap is that changing the system default every time you switch repositories…",
  "summary_source": "publisher",
  "key_points": [
    "Install Java 21 and Java 25 side by side on Ubuntu using package archive",
    "Understand three Java version management levels: system default, shell override, per-build pinning",
    "Diagnose version mismatches by checking system default, JAVAHOME, and build tool configurations"
  ],
  "editors_take": "Using these three management methods helps developers avoid version mismatches and tedious manual switching when working on different projects that require distinct Java releases.",
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}