Detect if Java application was run as a Windows admin

前端 未结 8 2064
天命终不由人
天命终不由人 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.

    0 讨论(0)
  • 2020-11-27 05:50

    Below code worked out for me

    Command prompt command

    net user
    

    Java code

    public static  boolean isAdmin() {
            StringBuilder outputbuilder = new StringBuilder();
        try {
            ProcessBuilder builder = new ProcessBuilder(
                    "cmd.exe","/c" ,"net user");
            builder.redirectErrorStream(true);
            Process p = builder.start();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while (true) {
                line = r.readLine();
                if (line == null) { break; }
                outputbuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        System.out.println(outputbuilder.toString());
        return outputbuilder.toString().contains("Administrator");
    }
    
    0 讨论(0)
  • 2020-11-27 05:54

    The method from the answer marked best worked nicely for me until the point when I had to build the code in Jenkins on a Linux machine. com.sun.security.auth.module.NTSystem() is not available there and using sun packages is generally considered a bad practice: link

    0 讨论(0)
  • 2020-11-27 05:57

    I've found a different solution that seems to be platform-independent. It tries to write system-preferences. If that fails, the user might not be an admin.

    As Tomáš Zato suggested, you might want to suppress error messages caused by this method. You can do this by setting System.err:

    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.util.prefs.Preferences;
    
    import static java.lang.System.setErr;
    import static java.util.prefs.Preferences.systemRoot;
    
    public class AdministratorChecker
    {
        public static final boolean IS_RUNNING_AS_ADMINISTRATOR;
    
        static
        {
            IS_RUNNING_AS_ADMINISTRATOR = isRunningAsAdministrator();
        }
    
        private static boolean isRunningAsAdministrator()
        {
            Preferences preferences = systemRoot();
    
            synchronized (System.err)
            {
                setErr(new PrintStream(new OutputStream()
                {
                    @Override
                    public void write(int b)
                    {
                    }
                }));
    
                try
                {
                    preferences.put("foo", "bar"); // SecurityException on Windows
                    preferences.remove("foo");
                    preferences.flush(); // BackingStoreException on Linux
                    return true;
                } catch (Exception exception)
                {
                    return false;
                } finally
                {
                    setErr(System.err);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 05:58

    Only by attempting an operation which requires such access (like binding a low-numbered port, or opening a known-to-be-protected file).

    0 讨论(0)
  • 2020-11-27 06:01

    There is not such a facility available in the Java Runtime Environment, but might be in a platform-dependent native routine. Note that usually the best way to be certain is to actually try to do it, and see if it fails.

    0 讨论(0)
提交回复
热议问题