Jdk source package

Hello, I am developing java and would like to tell my ide (intellij) where to find the sources for openjdk 8. Is there a package that contains them? If not, what should I preferrably do to link sources?

1 Like

Good question! Also what about javadocs? I think the setup would be similar. It would be nice to use a link to the file system rather than the web.

I did some investigations but have had no success yet.

The sources come from src.zip. This can be configured in the project settings, but should work without manual intervention, according to Jetbrains
openjdk in nixpkgs doesn’t contain src.zip because we delete it here
Maybe it’ll work, if we don’t delete it there.

If it is, however, a big file, we should probably put it into a different output of the derivation and then add that output to the Intellij wrapper.

I’m not sure, how Intellij determines the location of src.zip. Probably JAVA_HOME.

Like you said, @moaxcp, it’s possible to configure the documentation as a URL.
I cannot find a place, where the docs can be downloaded, other than for Oracle’s JDK, which requires a login: here

Do you have a dfifferent distro or a Windows machine and can check whether that Intellij installation can find the local docs by itself?

At work I use intellij on Red Hat distro I need to manually add docs. Usually there is a rpm for javadocs and src. I wonder if there could be an option for the jdk to keep src. Maybe the same could be done for javadoc.

And on RedHat Intellij finds the javadocs automatically?
Could you please look up to which paths, the Classpath, Sourcpath and Documentation Paths point to?
You can view it in the “Project Structure”:

Ye, we could add an option for src.zip, I’m investigating the best way to do so.

We could probably build the javadoc from the source, that we build the JDK from.
For now, I created a derivation which fetches the pre-built javadoc from Debian. If I import this into Intellij, it shows the documentation.

{ stdenv, fetchurl, dpkg }:
stdenv.mkDerivation rec {
  pname = "openjdk-doc";
  version = "11";

  src = fetchurl {
    name = "openjdk-${version}-doc.deb";
    url = "http://security.debian.org/debian-security/pool/updates/main/o/openjdk-11/openjdk-11-doc_11.0.5+10-1~deb10u1_all.deb";
    sha256 = "15md28g3z8fw1mp019r6pv7d4cmk2gh2rkl53jmizwjrp2lfapgm";
  };

  nativeBuildInputs = [ dpkg ];

  phases = [ "installPhase" ];

  installPhase = ''
    dpkg-deb -x $src $out
  '';
}

Build the derivation and set “Documentation Paths” to /nix/store/dqb3grdas2y0xrkzfi7i1im3ccivl671-openjdk-doc-11/usr/share/doc/openjdk-11-doc/api

On red hat it is not automatic. I have to install the package and point intellij to the dir. Similar to what you are doing.

This isn’t correct. The nix expression you’re citing is for the bootstrap JDK, i.e. the JDK that’s needed for the JDK to build itself. The JDK build process doesn’t need src.zip in its bootstrap JDK, that’s all.

There is a src.zip inside the actual JDK in the store:

$ file /nix/store/q2mivnykja644pzqvkfqllzqryia0y5n-openjdk-8u222-ga/lib/openjdk/src.zip 
/nix/store/q2mivnykja644pzqvkfqllzqryia0y5n-openjdk-8u222-ga/lib/openjdk/src.zip: Zip archive data, at least v2.0 to extract

Further, I use this approach to ensure the jdk is always available at a fixed path. So I have:

$ ls -l /etc/jdk*/lib/openjdk/{,lib/}src.zip 
-r--r--r-- 2 root root 65042839 Dec 31  1969 /etc/jdk12/lib/openjdk/lib/src.zip
-r--r--r-- 2 root root 52069750 Dec 31  1969 /etc/jdk8/lib/openjdk/src.zip
-r--r--r-- 2 root root 52069750 Dec 31  1969 /etc/jdk/lib/openjdk/src.zip
1 Like

From the image it looks like you are using jetbrains and the file at /nix/store/...gllcr-jetbrainsjdk-485.1 is the src.zip. I'm only guessing given the !` after. Is this not working for you?

Looking at the original question is a good idea! If it is not your JAVA_HOME but still installed you can do a search with find but if it is in your path you can use readlink.

using find:

$ find /nix/store -type d -name "*openjdk*"

using readlink

$ which javac
/home/john/.nix-profile/bin/javac
$ readlink $(which javac)
/nix/store/qzkq5cmi0i4jz9lxmdkvq6mml11y1sy1-adoptopenjdk-hotspot-bin-11.0.4/bin/javac

You can then add the directory as a new java platform in intellij.

If you are using Visual Code with metals, I’ve found that setting

JAVA_HOME=/nix/store/<openjdk-hash>/lib/openjdk

Seems to work. Having the lib/openjdk postfix is a bit weird though …

Hi,
in your configuration.nix you might want to add:
programs.java.enable = true;

The documentation states that JAVA_HOME is set when using this option, also for a custom java package as JAVA_HOME you can use the key: programs.java.package = pkgs.openjdk11 for example, to use java 11

https://nixos.org/nixos/options.html#programs.java.enable

https://nixos.org/nixos/options.html#programs.java.package

1 Like

Thanks, that was a really nice solution! I didn’t know that option existed. :slight_smile: