简体   繁体   English

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

[英]Run Java application as administrator on Windows

I am writing an installer in Java that will accordingly require elevated privileges to access the Program Files directory. 我正在用Java编写安装程序,因此需要提升权限才能访问Program Files目录。 Based on information that I've found online, I've written an implementation as follows: 根据我在网上找到的信息,我编写了如下实现:

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
    }
}

The test that I'm using to check for privileges is modified slightly from an answer found here and looks like this: 我用于检查权限的测试稍微修改一下,在这里找到的答案如下所示:

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;
}

When I run this, it fails the privileges test--as expected--and makes the call to exec. 当我运行它时,它失败了特权测试 - 正如预期的那样 - 并调用exec。 Checking if the call worked by looking at p.isAlive() shows that the process is, in fact, alive; 通过查看p.isAlive()检查调用是否有效,这表明该进程实际上是活着的; however, I'm not seeing any evidence of the new process and Windows isn't prompting me to grant permissions. 但是,我没有看到任何新进程的证据,Windows也没有提示我授予权限。

I'm not familiar with using exec() in Java, so it's quite possible that I've misunderstood its usage somehow. 我不熟悉在Java中使用exec() ,所以很可能我以某种方式误解了它的用法。 For that matter, is what I'm attempting to do here even possible? 就此而言,我试图在这里做甚么可能吗? If not, is there a straightforward alternative that will actually get me the results that I'm looking for? 如果没有,是否有一个直接的选择,实际上会得到我正在寻找的结果?

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. 从那里,问题是实际上获得使用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). checkPrivileges方法从上面保持不变, Elevator类几乎与链接解决方案中出现的相同(我刚刚取出了不需要的main方法)。 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. 改变这一点以满足您的个人需求应该不会太难。

I think that the best approach to achieve this is to use the tools that microsoft built for developers since you can't do it in simple Java. 我认为实现这一目标的最佳方法是使用microsoft为开发人员构建的工具,因为您无法在简单的Java中实现。 In this case use a manifest file http://msdn.microsoft.com/en-us/library/aa375632(v=vs.85).aspx Just build the wrapper .exe that asks for this privileges and then spawns your java program. 在这种情况下使用清单文件http://msdn.microsoft.com/en-us/library/aa375632(v=vs.85).aspx只需构建包装器.exe,请求此权限,然后生成您的java程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM