Java: Difference between revisions

Kvtb (talk | contribs)
JavaFX/Webkit
DHCP (talk | contribs)
m style fixes
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
This article is about [https://en.wikipedia.org/wiki/Java_(programming_language) Java], the programming language.
This article is about [[Wikipedia:Java (programming language)|Java]], the programming language.


__TOC__
__TOC__
Line 9: Line 9:
== JDK options ==
== JDK options ==


Your default choice should probably be to install <code>jdk</code>, which is an alias to the latest [https://en.wikipedia.org/wiki/Long-term_support 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.
Your default choice should probably be to install <code>jdk</code>, which is an alias to the latest [[Wikipedia:Long-term support|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.
Line 24: Line 24:


== VSCode + Language Support for Java (TM) by Red Hat extension ==
== 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 <code>flake.nix</code> snippet to get you started (I'll focus on the <code>devShell</code> part for brevity):
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/nix-community/nix-ld nix-ld]. Here's a simple <code>flake.nix</code> snippet to get you started (I'll focus on the <code>devShell</code> part for brevity):


<syntaxhighlight lang="nix" line highlight="6,10" copy>
{{file|flake.nix|lang=nix|3=
# flake.nix
devShell = pkgs.mkShell {
devShell = pkgs.mkShell {
   buildInputs = [
   buildInputs = [
Line 40: Line 39:
   # ^--- when using direnv, this line will require the 'use flake --impure' option.
   # ^--- when using direnv, this line will require the 'use flake --impure' option.
};
};
</syntaxhighlight>
}}


The important lines above are the two starting with <code>NIX_LD...</code>. They will let nix-ld wrap the required, dynamically loaded libraries so that they are found when building the <code>devShell</code>.  
The important lines above are the two starting with <code>NIX_LD...</code>. They will let nix-ld wrap the required, dynamically loaded libraries so that they are found when building the <code>devShell</code>.  
Line 50: Line 49:


programs.vscode.userSettings = {
programs.vscode.userSettings = {
   "java.jdt.ls.java.home" = "${pkgs.jdk17}";
   "java.jdt.ls.java.home" = "${pkgs.jdk17}/lib/openjdk";
};
};
</syntaxhighlight>Note that this will still result in the extension downloading its own JRE, it just will not be used.
</syntaxhighlight>Note that this will still result in the extension downloading its own JRE, it just will not be used.
Line 71: Line 70:
To install the Oracle JRE system-wide, you need to explicitly accept the license in addition to allowing unfree modules:
To install the Oracle JRE system-wide, you need to explicitly accept the license in addition to allowing unfree modules:


<syntaxhighlight lang="nix">
{{file|/etc/nixos/configuration.nix|lang=nix|3=
# /etc/nixos/configuration.nix
{
{
   nixpkgs.config.allowUnfree = true;
   nixpkgs.config.allowUnfree = true;
   programs.java = { enable = true; package = pkgs.oraclejre8; };
   programs.java = { enable = true; package = pkgs.oraclejre8; };
}
}
</syntaxhighlight>
}}


Working with <code>requireFile</code> (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
Working with <code>requireFile</code> (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
Line 114: Line 112:
</syntaxhighlight>
</syntaxhighlight>


More options can be found in the [https://wiki.archlinux.org/index.php/Java_Runtime_Environment_fonts archlinux wiki]
More options can be found in the [https://wiki.archlinux.org/title/Java_Runtime_Environment_fonts ArchWiki]


== Overriding java jks Certificate Store ==
== Overriding java jks Certificate Store ==
Line 151: Line 149:
=== jdk11 ===
=== jdk11 ===


JDK11 does not provide the cacert overridable and therefore it is not possible to use the same technique to override the truststore.
JDK11 does not provide the cacert overridable and therefore it is not possible to use the same technique to override the truststore.
 
As an alternative solution you can either set an environment variable, <code>JAVAX_NET_SSL_TRUSTSTORE</code>, or pass an argument to your program, <code>-Djavax.net.ssl.trustStore</code>, with the location of your cacert. See [https://discourse.nixos.org/t/custom-ssl-certificates-for-jdk/18297/9 discussion].


== Building and Packaging ==
== Building and Packaging ==
Line 161: Line 161:
[https://maven.apache.org/run.html Maven] is a build tool for Java. The typical build command is
[https://maven.apache.org/run.html Maven] is a build tool for Java. The typical build command is


<pre>mvn verify</pre>
<syntaxhighlight lang=console>
$ mvn verify
</syntaxhighlight>


[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
[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
Line 171: Line 173:
[https://ant.apache.org/manual/running.html Ant] is a build tool for Java. To build the <code>compile</code> target, run
[https://ant.apache.org/manual/running.html Ant] is a build tool for Java. To build the <code>compile</code> target, run


<pre>ant compile</pre>
<syntaxhighlight lang=console>
$ ant compile
</syntaxhighlight>


To list available build targets, run
To list available build targets, run


<pre>ant -p</pre>
<syntaxhighlight lang=console>
$ ant -p
</syntaxhighlight>


==== Ivy ====
==== Ivy ====


[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/by-name/iv/ivy/package.nix ivy] - an APL-like calculator


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 manually, see for example [https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/je/jedit/package.nix 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]
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]
Line 203: Line 209:
{{Nixpkgs manual|sec-language-java}}<br /><br /><br /><br /><br />{{Wikipedia|Java_(programming_language)}}
{{Nixpkgs manual|sec-language-java}}<br /><br /><br /><br /><br />{{Wikipedia|Java_(programming_language)}}


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