Search code examples
javawindowsadminprivileges

Detect if Java application was run as a Windows admin


I have a Java application. Is there anyway I can tell if the process was run with admin privileges, on Windows 7.


Solution

  • I found this code snippet online, that I think will do the job for you.

    public static boolean isAdmin() {
        String groups[] = (new com.sun.security.auth.module.NTSystem()).getGroupIDs();
        for (String group : groups) {
            if (group.equals("S-1-5-32-544"))
                return true;
        }
        return false;
    }
    

    It ONLY works on windows, and comes built in to the core Java package. I just tested this code and it does work. It surprised me, but it does.

    The SID S-1-5-32-544 is the id of the Administrator group in the Windows operating system.

    Here is the link for more details of how it works.