1# Java {#sec-language-java}
2
3Ant-based Java packages are typically built from source as follows:
4
5```nix
6stdenv.mkDerivation {
7 pname = "...";
8 version = "...";
9
10 src = fetchurl {
11 # ...
12 };
13
14 nativeBuildInputs = [
15 ant
16 jdk
17 stripJavaArchivesHook # removes timestamp metadata from jar files
18 ];
19
20 buildPhase = ''
21 runHook preBuild
22 ant # build the project using ant
23 runHook postBuild
24 '';
25
26 installPhase = ''
27 runHook preInstall
28
29 # copy generated jar file(s) to an appropriate location in $out
30 install -Dm644 build/foo.jar $out/share/java/foo.jar
31
32 runHook postInstall
33 '';
34}
35```
36
37Note that `jdk` is an alias for the OpenJDK (self-built where available,
38or pre-built via Zulu).
39
40Also note that not using `stripJavaArchivesHook` will likely cause the
41generated `.jar` files to be non-deterministic, which is not optimal.
42Using it, however, does not always guarantee reproducibility.
43
44JAR files that are intended to be used by other packages should be
45installed in `$out/share/java`. JDKs have a `stdenv` setup hook that adds
46any JARs in the `share/java` directories of the build inputs to the
47`CLASSPATH` environment variable. For instance, if the package `libfoo`
48installs a JAR named `foo.jar` in its `share/java` directory, and
49another package declares the attribute
50
51```nix
52{
53 buildInputs = [ libfoo ];
54 nativeBuildInputs = [ jdk ];
55}
56```
57
58then `CLASSPATH` will be set to
59`/nix/store/...-libfoo/share/java/foo.jar`.
60
61Private JARs should be installed in a location like
62`$out/share/package-name`.
63
64If your Java package provides a program, you need to generate a wrapper
65script to run it using a JRE. You can use `makeWrapper` for this:
66
67```nix
68{
69 nativeBuildInputs = [ makeWrapper ];
70
71 installPhase = ''
72 runHook preInstall
73
74 mkdir -p $out/bin
75 makeWrapper ${jre}/bin/java $out/bin/foo \
76 --add-flags "-cp $out/share/java/foo.jar org.foo.Main"
77
78 runHook postInstall
79 '';
80}
81```
82
83Since the introduction of the Java Platform Module System in Java 9,
84Java distributions typically no longer ship with a general-purpose JRE:
85instead, they allow generating a JRE with only the modules required for
86your application(s). Because we can't predict what modules will be
87needed on a general-purpose system, the default `jre` package is the full
88JDK. When building a minimal system/image, you can override the
89`modules` parameter on `jre_minimal` to build a JRE with only the
90modules relevant for you:
91
92```nix
93let
94 my_jre = pkgs.jre_minimal.override {
95 modules = [
96 # The modules used by 'something' and 'other' combined:
97 "java.base"
98 "java.logging"
99 ];
100 };
101 something = (pkgs.something.override { jre = my_jre; });
102 other = (pkgs.other.override { jre = my_jre; });
103in
104<...>
105```
106
107You can also specify what JDK your JRE should be based on, for example
108selecting a 'headless' build to avoid including a link to GTK+:
109
110```nix
111{ my_jre = pkgs.jre_minimal.override { jdk = jdk11_headless; }; }
112```
113
114Note all JDKs passthru `home`, so if your application requires
115environment variables like `JAVA_HOME` being set, that can be done in a
116generic fashion with the `--set` argument of `makeWrapper`:
117
118```bash
119--set JAVA_HOME ${jdk.home}
120```
121
122It is possible to use a different Java compiler than `javac` from the
123OpenJDK. For instance, to use the GNU Java Compiler:
124
125```nix
126{
127 nativeBuildInputs = [
128 gcj
129 ant
130 ];
131}
132```
133
134Here, Ant will automatically use `gij` (the GNU Java Runtime) instead of
135the OpenJRE.