Java: Difference between revisions

From NixOS Wiki
imported>Milahu
Ivy: not to be confused with ivy
→‎VSCode + Language Support for Java (TM) by Red Hat extension: Add alternate solution for using Language Support for Java
 
(11 intermediate revisions by 6 users not shown)
Line 9: Line 9:
== JDK options ==
== JDK options ==


Your default choice should probably be to install <code>jdk11</code>, which is an alias for <code>openjdk11</code>. If you're in a server environment, go for <code>jdk11_headless</code>. Java 11 is the currently-maintained LTS version of OpenJDK as of March 2021.
Your default choice should probably be to install <code>jdk</code>, which is an alias the latest LTS. If you're in a server environment, go for <code>jdk21_headless</code>. Java 21 is the currently-maintained LTS version of OpenJDK as of April 2024.


As you might expect, though, many flavors of Java are available in NixOS.
As you might expect, though, many flavors of Java are available in NixOS.


* OpenJDK, by far the most popular non-Oracle JVM implementation
* OpenJDK, by far the most popular non-Oracle JVM implementation
** <code>openjdk8[_headless]</code> for a legacy Java 8 VM required by some older apps
** <code>jdk8[_headless]</code> for a legacy Java 8 VM required by some older apps
** <code>openjdk11[_headless]</code>, the currently-supported LTS version of OpenJDK
** <code>jdk21[_headless]</code>, the currently-supported LTS version of OpenJDK
** <code>openjdk14[_headless]</code>, the current version of OpenJDK
** <code>jdk22[_headless]</code>, the current version of OpenJDK
* AdoptOpenJDK, prebuilt binaries for OpenJDK
* Temurin, formerly AdoptOpenJDK, prebuilt binaries for OpenJDK
** <code>adoptopenjdk-bin</code> points to the latest version of AdoptOpenJDK, which is version 11 at the time of writing.
** <code>temurin-bin</code> points to the latest version of Temurin, which is version 21 at the time of writing.
** <code>adoptopenjdk-jre-bin</code> is available if you want to avoid downloading the compiler and only require the runtime environment.
** <code>temurin-jre-bin</code> is available if you want to avoid downloading the compiler and only require the runtime environment.
** <code>adoptopenjdk{,-jre}-openj9-bin-{8,11,13}</code> same, but using [https://en.wikipedia.org/wiki/OpenJ9 Eclipse OpenJ9] instead of HotSpot as its JVM.
* JetBrains JDK (<code>jetbrains.jdk</code>), a fork of OpenJDK with modifications made by JetBrains
* JetBrains JDK (<code>jetbrains.jdk</code>), a fork of OpenJDK with modifications made by JetBrains
* Oracle's JDK (<code>oraclejdk</code>), only version 8 is available.
* Oracle's JDK (<code>oraclejdk</code>), only version 8 is available.
== VSCode + Language Support for Java (TM) by Red Hat extension ==
Unfortunately the extension contains and uses a version of the JRE which makes use of dynamically loaded libraries, which nix cannot accomodate out-of-the-box. Fortunately there's a simple solution in the use of [https://github.com/Mic92/nix-ld nix-ld]. Here's a simple flake.nix to get you started (I'll focus on the devShell part for brevity):<syntaxhighlight lang="nix">
devShell = pkgs.mkShell {
  buildInputs = [
    pkgs.gradle
    pkgs.jdk17
  ];
  NIX_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
    pkgs.stdenv.cc.cc
    pkgs.openssl
  ];
  NIX_LD = pkgs.lib.fileContents "${pkgs.stdenv.cc}/nix-support/dynamic-linker";
  # ^--- when using direnv, this line will require the 'use flake --impure' option.
};
</syntaxhighlight>The important lines are the two lines starting with "NIX_LD...". They will let nix-ld wrap the required, dynamically loaded libraries so that they are found when building the devShell.
Another solution is to use the <code>[https://github.com/redhat-developer/vscode-java?tab=readme-ov-file#supported-vs-code-settings java.jdt.ls.java.home]</code> VSCode setting to point to a nix-built Java 17. For example, using home-manager's settings: <syntaxhighlight lang="nix">
programs.vscode.enable = true;
programs.vscode.extensions = [ pkgs.vscode-extensions.redhat.java ];
programs.vscode.userSettings = {
  "java.jdt.ls.java.home" = "${pkgs.jdk17}";
};
</syntaxhighlight>Note that this will still result in the extension downloading its own JRE, it just will not be used.


== Using Oracle JDK instead of Open JDK ==
== Using Oracle JDK instead of Open JDK ==
Line 120: Line 145:
== Building and Packaging ==
== Building and Packaging ==


see the [https://ryantm.github.io/nixpkgs/languages-frameworks/java/ Java section in the Nixpkgs manual]
See the [https://nixos.org/manual/nixpkgs/#sec-language-java Java section in the Nixpkgs manual].


=== Maven ===
=== Maven ===
Line 128: Line 153:
<pre>mvn verify</pre>
<pre>mvn verify</pre>


[https://github.com/fzakaria/mvn2nix mvn2nix] and [https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/build-maven.nix buildMaven] can be used to build Maven projects with Nix
[https://github.com/fzakaria/mvn2nix mvn2nix], [https://nixos.org/manual/nixpkgs/stable/#maven-buildmavenpackage buildMavenPackage] (recommended) can be used to build Maven projects with Nix
 
See also: [https://fzakaria.com/2020/07/20/packaging-a-maven-application-with-nix.html Packaging a Maven application with Nix] and [https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ma/maven/build-maven-package.nix buildMavenPackage source]


=== Ant ===
=== Ant ===
Line 144: Line 171:
[https://ant.apache.org/ivy/ Ivy] is a package manager for Ant, not to be confused with [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/ivy/default.nix ivy] - an APL-like calculator
[https://ant.apache.org/ivy/ Ivy] is a package manager for Ant, not to be confused with [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/ivy/default.nix ivy] - an APL-like calculator


To replace ivy, see for example [https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/jedit pkgs/applications/editors/jedit]
To fetch ivy sources manually, see for example [https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/jedit pkgs/applications/editors/jedit]
 
To fetch ivy sources in a fixed-output-derivation, see for example [https://github.com/milahu/nur-packages/blob/master/pkgs/yacy/yacy.nix yacy.nix]


== Further reading ==
{{Nixpkgs manual|sec-language-java}}<br /><br /><br /><br /><br />{{Wikipedia|Java_(programming_language)}}


[[Category:Applications]]
[[Category:Applications]]
[[Category:Languages]]
[[Category:Languages]]

Latest revision as of 15:32, 14 October 2024

This article is about Java, the programming language.

Java Web Start

Available as javaws in package adoptopenjdk-icedtea-web.

JDK options

Your default choice should probably be to install jdk, which is an alias the latest LTS. If you're in a server environment, go for jdk21_headless. Java 21 is the currently-maintained LTS version of OpenJDK as of April 2024.

As you might expect, though, many flavors of Java are available in NixOS.

  • OpenJDK, by far the most popular non-Oracle JVM implementation
    • jdk8[_headless] for a legacy Java 8 VM required by some older apps
    • jdk21[_headless], the currently-supported LTS version of OpenJDK
    • jdk22[_headless], the current version of OpenJDK
  • Temurin, formerly AdoptOpenJDK, prebuilt binaries for OpenJDK
    • temurin-bin points to the latest version of Temurin, which is version 21 at the time of writing.
    • temurin-jre-bin is available if you want to avoid downloading the compiler and only require the runtime environment.
  • JetBrains JDK (jetbrains.jdk), a fork of OpenJDK with modifications made by JetBrains
  • Oracle's JDK (oraclejdk), only version 8 is available.

VSCode + Language Support for Java (TM) by Red Hat extension

Unfortunately the extension contains and uses a version of the JRE which makes use of dynamically loaded libraries, which nix cannot accomodate out-of-the-box. Fortunately there's a simple solution in the use of nix-ld. Here's a simple flake.nix to get you started (I'll focus on the devShell part for brevity):

devShell = pkgs.mkShell {
  buildInputs = [
    pkgs.gradle
    pkgs.jdk17
  ];
  NIX_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
    pkgs.stdenv.cc.cc
    pkgs.openssl
  ];
  NIX_LD = pkgs.lib.fileContents "${pkgs.stdenv.cc}/nix-support/dynamic-linker"; 
  # ^--- when using direnv, this line will require the 'use flake --impure' option.
};

The important lines are the two lines starting with "NIX_LD...". They will let nix-ld wrap the required, dynamically loaded libraries so that they are found when building the devShell. Another solution is to use the java.jdt.ls.java.home VSCode setting to point to a nix-built Java 17. For example, using home-manager's settings:

programs.vscode.enable = true;

programs.vscode.extensions = [ pkgs.vscode-extensions.redhat.java ];

programs.vscode.userSettings = {
  "java.jdt.ls.java.home" = "${pkgs.jdk17}";
};

Note that this will still result in the extension downloading its own JRE, it just will not be used.

Using Oracle JDK instead of Open JDK

Almost all Java packages in nixpkgs use Open JDK in form of a jre dependency. If you use Oracle JDK and also want other applications to use it, you can simply tweak your .nixpkgs/config.nix so that your desired application uses Oracles JDK or JRE.

Example with UMLet with JRE

{
  allowUnfree = true;
  packageOverrides = pkgs: rec {
    umlet = pkgs.umlet.override {
      jre = pkgs.oraclejre8;
    };
  };
}

To install the Oracle JRE system-wide, you need to explicitly accept the license in addition to allowing unfree modules:

# /etc/nixos/configuration.nix
{
  nixpkgs.config.allowUnfree = true;
  programs.java = { enable = true; package = pkgs.oraclejre8; };
}

Working with `requireFile` (manual downloading the tarballs and manual adding in to the nix store) might be annoying and nixops-unfriendly, so it can be overridden in overlays

nixpkgs.overlays = let
  files = {
    "jdk-8u241-linux-linux-arm32-vfp-hflt.tar.gz" = /home/user/blobs/java/jdk-8u241-linux-linux-arm32-vfp-hflt.tar.gz;
    "jdk-8u241-linux-linux-arm64-vfp-hflt.tar.gz" = /home/user/blobs/java/jdk-8u241-linux-linux-arm64-vfp-hflt.tar.gz;
    "jdk-8u241-linux-i586.tar.gz"                 = /home/user/blobs/java/jdk-8u241-linux-i586.tar.gz;
    "jdk-8u241-linux-x64.tar.gz"                  = /home/user/blobs/java/jdk-8u241-linux-x64.tar.gz;
  };
in [
  (self: super: {
    requireFile = args @ {name, url, sha1 ? null, sha256 ? null}:
      if files?${name} then
        self.stdenvNoCC.mkDerivation {
          inherit name;
          outputHashMode = "flat";
          outputHashAlgo = if sha256 != null then "sha256" else "sha1";
          outputHash     = if sha256 != null then  sha256  else  sha1 ;
          buildCommand   = "cp ${files.${name}} $out";
        }
      else
        super.requireFile args;
  })
];

Better font rendering

By default java does not enable antialiasing for font rendering. By exporting environment variables, this can be fixed:

$ export _JAVA_OPTIONS='-Dawt.useSystemAAFontSettings=lcd'

More options can be found in the archlinux wiki

Overriding java jks Certificate Store

Overriding the java certificate store may be required for adding your own Root certificates in case your company uses an internal PKI or the company utilizes an intercepting proxy.

jdk8

Overriding the jdk8 certificate store is possible by overriding the cacert parameter of the package:

{ pkgs, ... }:
let 
  myjdk = pkgs.jdk8.override {
    cacert = pkgs.runCommand "mycacert" {} ''
      mkdir -p $out/etc/ssl/certs
      cat ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt \
        ${./my-company-root-certificate.crt} > $out/etc/ssl/certs/ca-bundle.crt
    '';
  };
in {
  programs.java = {
    enable = true;
    package = myjdk
  };
}

the java package build will use the ca-bundle to run keytool and transform it into jks format.

you could also use

{
  nixpkgs.overlays = [(self: super: {jdk = super.jdk8.override { };} )];
}

to override the default jdk so all packages use the patched java version.

jdk11

JDK11 does not provide the cacert overridable and therefore it is not possible to use the same technique to override the truststore.

Building and Packaging

See the Java section in the Nixpkgs manual.

Maven

Maven is a build tool for Java. The typical build command is

mvn verify

mvn2nix, buildMavenPackage (recommended) can be used to build Maven projects with Nix

See also: Packaging a Maven application with Nix and buildMavenPackage source

Ant

Ant is a build tool for Java. To build the compile target, run

ant compile

To list available build targets, run

ant -p

Ivy

Ivy is a package manager for Ant, not to be confused with ivy - an APL-like calculator

To fetch ivy sources manually, see for example pkgs/applications/editors/jedit

To fetch ivy sources in a fixed-output-derivation, see for example yacy.nix

Further reading

The Nixpkgs manual has a section about Java.






English Wikipedia has an article about Java.