1<section xmlns="http://docbook.org/ns/docbook" 2 xmlns:xlink="http://www.w3.org/1999/xlink" 3 xml:id="sec-language-java"> 4 <title>Java</title> 5 6 <para> 7 Ant-based Java packages are typically built from source as follows: 8<programlisting> 9stdenv.mkDerivation { 10 name = "..."; 11 src = fetchurl { ... }; 12 13 buildInputs = [ jdk ant ]; 14 15 buildPhase = "ant"; 16} 17</programlisting> 18 Note that <varname>jdk</varname> is an alias for the OpenJDK (self-built 19 where available, or pre-built via Zulu). Platforms with OpenJDK not (yet) in 20 Nixpkgs (<literal>Aarch32</literal>, <literal>Aarch64</literal>) point to the 21 (unfree) <literal>oraclejdk</literal>. 22 </para> 23 24 <para> 25 JAR files that are intended to be used by other packages should be installed 26 in <filename>$out/share/java</filename>. JDKs have a stdenv setup hook that 27 add any JARs in the <filename>share/java</filename> directories of the build 28 inputs to the <envar>CLASSPATH</envar> environment variable. For instance, if 29 the package <literal>libfoo</literal> installs a JAR named 30 <filename>foo.jar</filename> in its <filename>share/java</filename> 31 directory, and another package declares the attribute 32<programlisting> 33buildInputs = [ jdk libfoo ]; 34</programlisting> 35 then <envar>CLASSPATH</envar> will be set to 36 <filename>/nix/store/...-libfoo/share/java/foo.jar</filename>. 37 </para> 38 39 <para> 40 Private JARs should be installed in a location like 41 <filename>$out/share/<replaceable>package-name</replaceable></filename>. 42 </para> 43 44 <para> 45 If your Java package provides a program, you need to generate a wrapper 46 script to run it using the OpenJRE. You can use 47 <literal>makeWrapper</literal> for this: 48<programlisting> 49buildInputs = [ makeWrapper ]; 50 51installPhase = 52 '' 53 mkdir -p $out/bin 54 makeWrapper ${jre}/bin/java $out/bin/foo \ 55 --add-flags "-cp $out/share/java/foo.jar org.foo.Main" 56 ''; 57</programlisting> 58 Note the use of <literal>jre</literal>, which is the part of the OpenJDK 59 package that contains the Java Runtime Environment. By using 60 <literal>${jre}/bin/java</literal> instead of 61 <literal>${jdk}/bin/java</literal>, you prevent your package from depending 62 on the JDK at runtime. 63 </para> 64 65 <para> 66 Note all JDKs passthru <literal>home</literal>, so if your application 67 requires environment variables like <envar>JAVA_HOME</envar> being set, that 68 can be done in a generic fashion with the <literal>--set</literal> argument 69 of <literal>makeWrapper</literal>: 70<programlisting> 71 --set JAVA_HOME ${jdk.home} 72</programlisting> 73 </para> 74 75 <para> 76 It is possible to use a different Java compiler than <command>javac</command> 77 from the OpenJDK. For instance, to use the GNU Java Compiler: 78<programlisting> 79buildInputs = [ gcj ant ]; 80</programlisting> 81 Here, Ant will automatically use <command>gij</command> (the GNU Java 82 Runtime) instead of the OpenJRE. 83 </para> 84</section>