在Windows上以管理员身份运行Java应用程序

bio*_*ion 5 java windows uac elevated-privileges

我正在用Java编写安装程序,因此需要提升权限才能访问Program Files目录.根据我在网上找到的信息,我编写了如下实现:

public static void main(String args[]) {
    if (!checkPrivileges()) { // spawn a copy w/ elevated privileges
        Runtime runtime = Runtime.getRuntime();
        try {
            Process p = runtime.exec(
                "runas /profile /user:Administrator \"java -cp . main.Main\"");
        } catch (IOException e) { ... }
    } else {
        // Run with elevated privileges
    }
}
Run Code Online (Sandbox Code Playgroud)

我用于检查权限的测试稍微修改一下,在这里找到的答案如下所示:

private static boolean checkPrivileges() {
    File testPriv = new File("C:\\Program Files\\");
    if (!testPriv.canWrite()) return false;
    File fileTest = null;
    try {
        fileTest = File.createTempFile("test", ".dll", testPriv);
    } catch (IOException e) {
        return false;
    } finally {
        if (fileTest != null)
            fileTest.delete();
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它失败了特权测试 - 正如预期的那样 - 并调用exec.通过查看检查呼叫是否有效p.isAlive()表明该过程实际上是活着的; 但是,我没有看到任何新进程的证据,Windows也没有提示我授予权限.

我不熟悉exec()在Java中的使用,所以我很可能以某种方式误解了它的用法.就此而言,我试图在这里做甚么可能吗?如果没有,是否有一个直接的选择,实际上会得到我正在寻找的结果?

bio*_*ion 1

好吧,我终于找到了一个令我满意的解决方案;这有点难看,但它适合我正在做的事情。

我借用了这个答案中的代码来进行实际的权限提升;从那时起,问题之一就是如何让该解决方案真正与 Java 一起使用。其代码最终如下所示:

    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
    }
Run Code Online (Sandbox Code Playgroud)

checkPrivileges方法与上面相同,并且该类Elevator实际上与链接解决方案中出现的类相同(我只是取出了不需要的main方法)。该方案假设要提升的进程是一个jar;改变它以满足您的个人需求应该不会太困难。