I created an implementation which does not use either systemd-notify as a subprocess nor require JNA/JNI. However, as Java does not support AF_UNIX Datagram sockets natively I had to pull in junixsocket as a depenency (though this can likely be replaced with a smaller library, it was just the first one which I found).
import java.io.IOException;
import java.nio.ByteBuffer;
import org.newsclub.net.unix.AFUNIXDatagramSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
public class SdNotify {
public static void main(String[] args) {
ready();
}
public static void ready() {
notify("READY=1");
}
public static void status(String status) {
notify("STATUS=" + status);
}
/// ...implement other well-known sd_notify(3) messages as needed...
private static void notify(String arg) {
var socketPath = System.getenv("NOTIFY_SOCKET");
if (socketPath == null)
return;
if (!(socketPath.startsWith("/") || socketPath.startsWith("@"))) {
throw new UnsupportedOperationException("Unsupported socket type");
}
if (socketPath.startsWith("@")) {
socketPath = "\0" + socketPath.substring(1);
}
try (var socket = AFUNIXDatagramSocket.newInstance()) {
socket.connect(AFUNIXSocketAddress.of(socketPath.getBytes()));
try (var out = socket.getChannel()) {
out.write(ByteBuffer.wrap(arg.getBytes()));
}
} catch (IOException e) {
System.err.printf("Failed to notify systemd of service readiness: %s", e.toString());
}
}
}
https://gist.github.com/septatrix/dcb1d7763c941be399da5dc97397e6c3