1 # Java {#sec-language-java}
3 Ant-based Java packages are typically built from source as follows:
10 src = fetchurl { /* ... */ };
15 stripJavaArchivesHook # removes timestamp metadata from jar files
20 ant # build the project using ant
27 # copy generated jar file(s) to an appropriate location in $out
28 install -Dm644 build/foo.jar $out/share/java/foo.jar
35 Note that `jdk` is an alias for the OpenJDK (self-built where available,
36 or pre-built via Zulu).
38 Also note that not using `stripJavaArchivesHook` will likely cause the
39 generated `.jar` files to be non-deterministic, which is not optimal.
40 Using it, however, does not always guarantee reproducibility.
42 JAR files that are intended to be used by other packages should be
43 installed in `$out/share/java`. JDKs have a stdenv setup hook that add
44 any JARs in the `share/java` directories of the build inputs to the
45 `CLASSPATH` environment variable. For instance, if the package `libfoo`
46 installs a JAR named `foo.jar` in its `share/java` directory, and
47 another package declares the attribute
51 buildInputs = [ libfoo ];
52 nativeBuildInputs = [ jdk ];
56 then `CLASSPATH` will be set to
57 `/nix/store/...-libfoo/share/java/foo.jar`.
59 Private JARs should be installed in a location like
60 `$out/share/package-name`.
62 If your Java package provides a program, you need to generate a wrapper
63 script to run it using a JRE. You can use `makeWrapper` for this:
67 nativeBuildInputs = [ makeWrapper ];
71 makeWrapper ${jre}/bin/java $out/bin/foo \
72 --add-flags "-cp $out/share/java/foo.jar org.foo.Main"
77 Since the introduction of the Java Platform Module System in Java 9,
78 Java distributions typically no longer ship with a general-purpose JRE:
79 instead, they allow generating a JRE with only the modules required for
80 your application(s). Because we can't predict what modules will be
81 needed on a general-purpose system, the default jre package is the full
82 JDK. When building a minimal system/image, you can override the
83 `modules` parameter on `jre_minimal` to build a JRE with only the
84 modules relevant for you:
88 my_jre = pkgs.jre_minimal.override {
90 # The modules used by 'something' and 'other' combined:
95 something = (pkgs.something.override { jre = my_jre; });
96 other = (pkgs.other.override { jre = my_jre; });
101 You can also specify what JDK your JRE should be based on, for example
102 selecting a 'headless' build to avoid including a link to GTK+:
106 my_jre = pkgs.jre_minimal.override {
107 jdk = jdk11_headless;
112 Note all JDKs passthru `home`, so if your application requires
113 environment variables like `JAVA_HOME` being set, that can be done in a
114 generic fashion with the `--set` argument of `makeWrapper`:
117 --set JAVA_HOME ${jdk.home}
120 It is possible to use a different Java compiler than `javac` from the
121 OpenJDK. For instance, to use the GNU Java Compiler:
125 nativeBuildInputs = [ gcj ant ];
129 Here, Ant will automatically use `gij` (the GNU Java Runtime) instead of