Java可以启动Windows UAC吗? [英] Can Java launch the Windows UAC?

查看:148
本文介绍了Java可以启动Windows UAC吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想知道用Java编写的程序(且只有java )是否有可能以管理员权限重新启动自己(最好是.jar),显示在本机Windows UAC的方式(为了让它对用户更容易信任),我完成了我的功课,发现使用c ++和java之间的桥梁可以实现这一点,但我真的希望这样做是纯粹的java项目。

As the title says, I'm wondering if it is possible for a program written in Java (and only java) to relaunch himself (preferably a .jar) with administrator privileges, showing in the way the native Windows UAC (in order to make it more trustable for the user), i did my homework and found out that it is possible to accomplish this using bridges between c++ and java, but i would really like to do this as a pure java project.

PS:在远程情况下,这个结果是不可能的,有人可以用另一种语言向我展示简单的方法(我的意思是,我已经找到了教程,但是它们很复杂,我认为它不应该那么复杂。)

P.S: In the remote case that this result to be impossible, can someone show me the "easy" way to do this using another language (i mean, I've found tutorials, but they are to complicated for something I think it should not be that complicated).

P.S2:如果有可能实现这个目的它会在其他平台上运行吗(OS X,Linux)

P.S2: In case it is possible to accomplish this, would it work, on other platforms (OS X, Linux)

推荐答案

在纯java中无法完成。

It cannot be done in pure java.

最好的办法是将此内容写入文件:

Best bet would be to write this to a file:

@echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
@echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
@echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
@cscript %temp%\sudo.tmp.vbs

并将其保存为Windows temp 目录中的 something.bat (因为我们可以访问它)。

and save it as something.bat in Windows temp directory (as we have access to this).

然后,您将使用 Runtime ProcessBuilder 从您的应用程序执行此操作并退出你的申请( System.exit(0); )。

You would then execute this from your application using Runtime or ProcessBuilder and exit your application (System.exit(0);).

你应该立即为你的启动检查添加检查程序是否有高程的应用程序,如果它没有重新运行批处理并且退出则继续。

You should add an immediate start up check to your application that checks if the program has elevation, if it has proceed if not re-run the batch and exit.

这是我做的一个例子(这必须在编译为Jar或它不会工作):

Here is an example I made (this must be run when compiled as a Jar or it wont work):

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author David
 */
public class UacTest {

    public static String jarName = "UacTest.jar", batName = "elevate.bat";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (checkForUac()) {//uac is on
            JOptionPane.showMessageDialog(null, "I am not elevated");
            //attempt elevation
            new UacTest().elevate();
            System.exit(0);
        } else {//uac is not on
            //if we get here we are elevated
            JOptionPane.showMessageDialog(null, "I am elevated");
        }

    }

    private static boolean checkForUac() {
        File dummyFile = new File("c:/aaa.txt");
        dummyFile.deleteOnExit();

        try {
            //attempt to craete file in c:/
            try (FileWriter fw = new FileWriter(dummyFile, true)) {
            }
        } catch (IOException ex) {//we cannot UAC muts be on
            //ex.printStackTrace();
            return true;
        }
        return false;
    }

    private void elevate() {
        //create batch file in temporary directory as we have access to it regardless of UAC on or off
        File file = new File(System.getProperty("java.io.tmpdir") + "/" + batName);
        file.deleteOnExit();

        createBatchFile(file);

        runBatchFile();

    }

    private String getJarLocation() {
        return getClass().getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);
    }

    private void runBatchFile() {
        //JOptionPane.showMessageDialog(null, getJarLocation());

        Runtime runtime = Runtime.getRuntime();
        String[] cmd = new String[]{"cmd.exe", "/C",
            System.getProperty("java.io.tmpdir") + "/" + batName + " java -jar " + getJarLocation()};
        try {
            Process proc = runtime.exec(cmd);
            //proc.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void createBatchFile(File file) {
        try {
            try (FileWriter fw = new FileWriter(file, true)) {
                fw.write(
                        "@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
                        + "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
                        + "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
                        + "@cscript %temp%\\sudo.tmp.vbs\r\n"
                        + "del /f %temp%\\sudo.tmp.vbs\r\n");
            }
        } catch (IOException ex) {
            //ex.printStackTrace();
        }
    }
}

这篇关于Java可以启动Windows UAC吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆