Skip to content

SoftwareOrgMx/Java-Is-Admin-or-Not

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Java-Is-Admin-or-Not

Check if the Java App is running in Admin or not.

import java.io.PrintStream;
import java.util.prefs.Preferences;

/**
 *
 * @author SoftwareOrgMX
 */
public class Main {

    public static void main(String[] args) {
        System.out.println("Is Admin: " + isAdmin());
    }

    public static boolean isAdmin() {

        Preferences prefs = Preferences.systemRoot();
        PrintStream systemErr = System.err;
        synchronized (systemErr) {    // better synchroize to avoid problems with other threads that access System.err
            System.setErr(null);
            try {
                prefs.put("foo", "bar"); // SecurityException on Windows
                prefs.remove("foo");
                prefs.flush(); // BackingStoreException on Linux
                return true;
            } catch (Exception e) {
                return false;
            } finally {
                System.setErr(systemErr);
            }
        }
    }
}

About

Check if the Java App is running in Admin or not.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages