How to get Windows service username in Java?

If I run a Java process as background service then System.getProperty("user.name") returns ever the hostname with an additional $ character.

Is there a solution to get the real user name of the process which I can see in the Windows task manager?

Answer

You can obtain the user who owns the current Java process using ProcessHandle.current().info().user().get(). It should be the same information provided by the system’s task list.

The documentation for ProcessHandle is here.

Leave a Reply

Your email address will not be published. Required fields are marked *