How to install multiple versions of jdk and specify a default?

Is it possible to install multiple stable versions of the openjdk and specify a default version to link?

Ideally, I’d have 8 (for legacy work), 11, and 14 when that’s available. Specifying multiple versions in a buildEnv, however causes priority conflicts (e.g., for /bin/unpack200).

I can create a shell.nix, of course, as needed which bypasses this issue.

When I was using macports it had this feature where you can ‘activate’ a specific version of a port.

sudo port select --set python python34

But other versions were still usable (e.g., python33). They just wouldn’t be default.

So in the case of the jdk, is this achievable or not recommended?

This is what I do, and works pretty well for me. I even have a project where I need jdk8 and jdk11 side-by-side, but that also works pretty easily:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    // some other dependencies
  ];

  JAVA_8_HOME = "${pkgs.jdk}/lib/openjdk";
  JAVA_11_HOME = "${pkgs.jdk11}/lib/openjdk";
}

(the project knows how to use those environment variables to find the JDK’s)

2 Likes

Thanks @raboof, that is brilliant!