How can I get eclipse-java with a specific JDK?

I have some projects that require JDK 14 and that I’d like to develop in Eclipse.

I’ve created a shell.nix file with this content:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = with pkgs; [
    eclipses.eclipse-java
    jdk14

    # keep this line if you use bash
    bashInteractive
  ];
}

This makes JDK14 available:

nix-shell --command "which java"
# /nix/store/swbd0b2gprpid3ksa5wgw0ablmqr2bkk-openjdk-14.0.2-ga/bin/java

nix-shell --command "which javac"
# /nix/store/swbd0b2gprpid3ksa5wgw0ablmqr2bkk-openjdk-14.0.2-ga/bin/javac

nix-shell --command "echo $JAVA_HOME"
# /nix/store/swbd0b2gprpid3ksa5wgw0ablmqr2bkk-openjdk-14.0.2-ga/lib/openjdk

But Eclipse in that shell doesn’t know about it. Each time I create a new workspace, I have to

  • go to menu Window > Preferences
  • in the Preferences, open Java > Installed JREs
  • there
    • optionally delete the pre-configure JDK 11 (/nix/store/352y6g6f8x3hvm68ddhwk2qffaig14l2-openjdk-11.0.9+11/lib/openjdk)
    • add a “Standard VM” with /nix/store/swbd0b2gprpid3ksa5wgw0ablmqr2bkk-openjdk-14.0.2-ga/lib/openjdk
  • in the Preferences, open Java > Installed JREs > Execution Environments
  • there
    • set the newly added VM for at least JavaSE-14
    • optionally set it also for the other predefined execution environments

Is there a way to avoid this manual setup?

Inspired by

I tried with

{ pkgs ? import <nixpkgs> {
  overlays = [ (self: super: {
    jdk = super.jdk14;
  }) ];
} }:

pkgs.mkShell {
  buildInputs = with pkgs; [
    eclipses.eclipse-java
    jdk

    # keep this line if you use bash
    bashInteractive
  ];
}

and

{ pkgs ? import <nixpkgs> {} }:

let
  pkgs = import <nixpkgs> { overlays = [ (self: super: {
    jdk = super.adoptopenjdk-jre-openj9-bin-11;
  }) ]; };
in
pkgs.mkShell {
  buildInputs = with pkgs; [
    eclipses.eclipse-java
    jdk

    # keep this line if you use bash
    bashInteractive
  ];
}

But neither seems to change which JDK is pre-configured in Eclipse, nor what the environment set in its wrapper script is:

nix-shell --command "cat $(which eclipse)"
#! /nix/store/516z50fm1jbpcl32qnzy7kynrh0vl22w-bash-4.4-p23/bin/bash -e
export PATH='/nix/store/352y6g6f8x3hvm68ddhwk2qffaig14l2-openjdk-11.0.9+11/bin'${PATH:+':'}$PATH
export LD_LIBRARY_PATH='/nix/store/0ds5gvys9awz8ab2mybyfhy7532yrhxa-glib-2.66.2/lib:/nix/store/lbaq8v4r1vzbadw02j0adfhin00pyyc8-gtk+3-3.24.23/lib:/nix/store/izrgpwzy91jg9mpqprs14v7vycqq39z8-libXtst-1.2.3/lib:/nix/store/krn2r4jnhdw5a2ppz2zhfmra63kd9wkz-webkitgtk-2.30.3/lib'${LD_LIBRARY_PATH:+':'}$LD_LIBRARY_PATH
export XDG_DATA_DIRS='/nix/store/rhyybrmwif0jkzbdx6fxszxinfl8qpl5-gsettings-desktop-schemas-3.38.0/share/gsettings-schemas/gsettings-desktop-schemas-3.38.0:/nix/store/lbaq8v4r1vzbadw02j0adfhin00pyyc8-gtk+3-3.24.23/share/gsettings-schemas/gtk+3-3.24.23'${XDG_DATA_DIRS:+':'}$XDG_DATA_DIRS
exec "/nix/store/n31ln3p9p6rfpdpg4qask5ngb9p623xj-eclipse-java-4.17/eclipse/eclipse"  -configuration $HOME/.eclipse/org.eclipse.platform_4.17.0/configuration "$@"

(I don’t really care what JDK / JRE is used to run eclipse, as long as it works. I just want different defaults for freshly created Eclipse workspaces.)

The eclipse default is to use a jvm that is included in eclipse. This behavior is set in the eclipse config file. From the eclipse help about changing settings:

On macOS, you start Eclipse by double clicking the Eclipse application. If you need to pass arguments to Eclipse, you’ll have to edit the eclipse.ini file inside the Eclipse application bundle: select the Eclipse application bundle icon while holding down the Control Key. This will present you with a popup menu. Select “Show Package Contents” in the popup menu. Locate eclipse.ini file in the Contents/MacOS sub-folder and open it with your favorite text editor to edit the command line options.

You would need to find a way to alter that file, as this is afaik the way eclipse foundation supposes (see: here) but for your situation adding a command line arg for -vm may be easier:
https://wiki.eclipse.org/FAQ_How_do_I_run_Eclipse%3F

1 Like

The reason your overrides aren’t working is because of this in pkgs/top-level/all-packages.nix:

eclipses = recurseIntoAttrs (callPackage ../applications/editors/eclipse {
  jdk = jdk11;
});

I’m sadly deficient in the ways of overlays and overrides and overloads (I always just edit nixpkgs directly), but if you can make that jdk = jdk14, that should do the trick.

1 Like

What I ended up doing is just deleting the file where eclipse stores what JDK version it is aware of and setting an alias. I would prefer to set the JDK globally with an overlay but this kind of works too and that way at least eclipse does not complain if the nix store path to the JDK changes.

The relevant shellHook

shellHook = ''
  WORKING_DIR=$PWD
  rm $WORKING_DIR/workspace/.metadata/.plugins/org.eclipse.jdt.launching/libraryInfos.xml
  alias eclipse="eclipse -vm ${pkgs.adoptopenjdk-openj9-bin-16}/bin/java"
'';