Detect if Java application was run as a Windows admin
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.
Java on Windows: Test if a Java application is run as an elevated process (with Administrator privileges)
This is what the Eclipse LocationManager
does to determine if it can write to the install directory:
public static boolean canWrite(File installDir) {
if (installDir.canWrite() == false)
return false;
if (!installDir.isDirectory())
return false;
File fileTest = null;
try {
// we use the .dll suffix to properly test on Vista virtual directories
// on Vista you are not allowed to write executable files on virtual directories like "Program Files"
fileTest = File.createTempFile("writtableArea", ".dll", installDir);
} catch (IOException e) {
//If an exception occured while trying to create the file, it means that it is not writable
return false;
} finally {
if (fileTest != null)
fileTest.delete();
}
return true;
}
Note the attempt to create a dll
How to determine if java program is running as administrator at run time
I used the reg query approach to determine if the current program is admin access or not. The following should give enough info that you can modify it for your needs.
try {
String command = "reg query \"HKU\\S-1-5-19\"";
Process p = Runtime.getRuntime().exec(command);
p.waitFor(); // Wait for for command to finish
int exitValue = p.exitValue(); // If exit value 0, then admin user.
if (0 == exitValue) {
System.out.println("admin user");
} else {
System.out.println("non admin user");
}
} catch (Exception e) {
e.printStackTrace();
}
Finding if the currently logged in user is an administrator or not in Windows OS
Java is cross platform and there is no way to see directly whether the user is a Windows administrator.
In general you should check for the priviledges you need instead of relying on some knowledge about what it means to be an administrator on Windows. For example, if you need to write to a file, check directly that the file is writeable etc...
Detect if user is admin
I don't think is possible to be totally OS independent, but a few months ago I had to check IzPack source code and it does exactly what you need.
In the PrivilegedRunner
class it has to check if it has admin privileges, check the method isElevationNeeded
Here is the source code
Detect if user is admin
I don't think is possible to be totally OS independent, but a few months ago I had to check IzPack source code and it does exactly what you need.
In the PrivilegedRunner
class it has to check if it has admin privileges, check the method isElevationNeeded
Here is the source code
Run Java application as administrator on Windows
Okay, I've finally managed to get a solution for this problem that I'm happy with; it's a bit on the ugly side, but it works for what I'm doing.
I borrowed the code from this answer to do the actual privilege elevation; from there, the question was one of actually getting that solution to work with Java. The code for that ends up looking like this:
if (!checkPrivileges()) {
try {
String jarPath = DownloaderMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(jarPath, "UTF-8");
decodedPath = decodedPath.substring(1, decodedPath.length());
Elevator.executeAsAdministrator(System.getProperty("java.home") + "\\bin\\java", "-jar " + "\"" + decodedPath + "\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
// Run with elevated privileges
}
The checkPrivileges
method is unchanged from above and the Elevator
class is virtually identical to the one that appears in the linked solution (I just took out the unneeded main
method). This solution assumes that the process to be elevated is a jar; it shouldn't be too difficult to change this around to suit your individual needs.
How to enforce packaged java app to run as admin
SOLVED by adding preinstall script to the javapackager. the script will run as root at deploy time giving me control of prepraing anything the app needs at runtime.
Related Topics
Java - Scroll to Specific Text Inside Jtextarea
JPA Query.Getresultlist() - Use in a Generic Way
Pdfbox Encode Symbol Currency Euro
Drag and Drop Custom Object from Jlist into Jlabel
Managing Dll Dependencies with Maven
Netbeans - Error: Could Not Find or Load Main Class
How to Include External Jar on My Netbeans Project
Java String Concatenation with + Operator
In Java How to Re-Open System.In After Closing It
How to Add Close Button to a Jtabbedpane Tab
Scanner Only Reads File Name and Nothing Else
Zoom Box for Area Around Mouse Location on Screen
Populate Jfreechart Timeseriescollection from MySQL Db
How to Listen for Key Presses (Within Java Swing) Across All Components