nixpkgs/doc/languages-frameworks/java.xml

78 lines
3.4 KiB
XML
Raw Normal View History

<section xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:id="sec-language-java">
2018-05-02 00:54:21 +01:00
<title>Java</title>
2018-05-02 00:54:21 +01:00
<para>
Ant-based Java packages are typically built from source as follows:
<programlisting>
stdenv.mkDerivation {
name = "...";
src = fetchurl { ... };
nativeBuildInputs = [ jdk ant ];
buildPhase = "ant";
}
</programlisting>
2019-09-18 21:12:54 +01:00
Note that <varname>jdk</varname> is an alias for the OpenJDK (self-built where available, or pre-built via Zulu). Platforms with OpenJDK not (yet) in Nixpkgs (<literal>Aarch32</literal>, <literal>Aarch64</literal>) point to the (unfree) <literal>oraclejdk</literal>.
2018-08-28 00:54:41 +01:00
</para>
2018-05-02 00:54:21 +01:00
<para>
2019-09-18 21:12:54 +01:00
JAR files that are intended to be used by other packages should be installed in <filename>$out/share/java</filename>. JDKs have a stdenv setup hook that add any JARs in the <filename>share/java</filename> directories of the build inputs to the <envar>CLASSPATH</envar> environment variable. For instance, if the package <literal>libfoo</literal> installs a JAR named <filename>foo.jar</filename> in its <filename>share/java</filename> directory, and another package declares the attribute
<programlisting>
buildInputs = [ libfoo ];
nativeBuildInputs = [ jdk ];
</programlisting>
2019-09-18 21:12:54 +01:00
then <envar>CLASSPATH</envar> will be set to <filename>/nix/store/...-libfoo/share/java/foo.jar</filename>.
2018-05-02 00:54:21 +01:00
</para>
<para>
2019-09-18 21:12:54 +01:00
Private JARs should be installed in a location like <filename>$out/share/<replaceable>package-name</replaceable></filename>.
2018-05-02 00:54:21 +01:00
</para>
<para>
If your Java package provides a program, you need to generate a wrapper script to run it using a JRE. You can use <literal>makeWrapper</literal> for this:
<programlisting>
nativeBuildInputs = [ makeWrapper ];
installPhase =
''
mkdir -p $out/bin
makeWrapper ${jre}/bin/java $out/bin/foo \
--add-flags "-cp $out/share/java/foo.jar org.foo.Main"
'';
</programlisting>
Since the introduction of the Java Platform Module System in Java 9, Java distributions typically no longer ship with a general-purpose JRE: instead, they allow generating a JRE with only the modules required for your application(s). Because we can't predict what modules will be needed on a general-purpose system, the default <package>jre</package> package is the full JDK. When building a minimal system/image, you can override the <literal>modules</literal> parameter on <literal>jre_minimal</literal> to build a JRE with only the modules relevant for you:
<programlisting>
let
my_jre = pkgs.jre_minimal.override {
modules = [
# The modules used by 'something' and 'other' combined:
"java.base"
"java.logging"
];
};
something = (pkgs.something.override { jre = my_jre; });
other = (pkgs.other.override { jre = my_jre; });
in
...
</programlisting>
2018-08-28 00:54:41 +01:00
</para>
2018-08-28 00:54:41 +01:00
<para>
2019-09-18 21:12:54 +01:00
Note all JDKs passthru <literal>home</literal>, so if your application requires environment variables like <envar>JAVA_HOME</envar> being set, that can be done in a generic fashion with the <literal>--set</literal> argument of <literal>makeWrapper</literal>:
<programlisting>
2019-06-17 11:01:51 +01:00
--set JAVA_HOME ${jdk.home}
</programlisting>
2018-08-28 00:54:41 +01:00
</para>
2018-05-02 00:54:21 +01:00
<para>
2019-09-18 21:12:54 +01:00
It is possible to use a different Java compiler than <command>javac</command> from the OpenJDK. For instance, to use the GNU Java Compiler:
<programlisting>
nativeBuildInputs = [ gcj ant ];
</programlisting>
2019-09-18 21:12:54 +01:00
Here, Ant will automatically use <command>gij</command> (the GNU Java Runtime) instead of the OpenJRE.
2018-05-02 00:54:21 +01:00
</para>
</section>