I have used this before:
var uidStr = Files.readString(Paths.get("/proc/self/loginuid"));
Example with jshell:
$ jshell
| Welcome to JShell -- Version 21.0.2
| For an introduction type: /help intro
jshell> Files.readString(Paths.get("/proc/self/loginuid"));
$1 ==> "226"