Detect if Java application was run as a Windows admin

前端 未结 8 2081
天命终不由人
天命终不由人 2020-11-27 05:37

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

8条回答
  •  余生分开走
    2020-11-27 05:49

    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.

提交回复
热议问题