如何在 Java 中使用 'run as' 动词请求提升

How to request elevation with the 'run as' verb in Java

我试过 运行 这个 Java 应用程序:

public class Run_Process {

    public static void main(String[] args) {
        String myCommandIp = "netsh interface ip set address name=\"Ethernet\" static 192.168.0.4 255.255.255.0 192.168.0.1 1";
        String myCommand2Dns = "netsh interface ipv4 add dnsserver \"Ethernet\" address=192.168.0.1 index=1";

        runCommand(myCommandIp);
        runCommand(myCommand2Dns);
    }

    static void runCommand(String command) {
        try {
            new ProcessBuilder(command.split(" ")).redirectError(ProcessBuilder.Redirect.INHERIT)
                    .redirectOutput(ProcessBuilder.Redirect.INHERIT).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

但我得到:

The requested operation requires elevation(Run as administrator)

如何再次启动我的应用程序,使用 'run as' 动词请求提升?这就是我在 Python 中的做法。但是,我需要帮助才能在 Java.

中做到这一点
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import ctypes, sys, subprocess

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    subprocess.call(['wmic', '/output:usracc.txt', 'useraccount', 'list', 'full', '/format:csv'])
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

这个问题已经被问过很多次了,但我需要一个具体的例子来说明我的情况,因为我是菜鸟。

我将解释为什么这不是链接问题的重复。使用快捷方式不是一种选择。我必须假设用户不知道如何创建快捷方式。 JNAwizmo的解决办法就不解释了。 Here 它说:

So, in the Java world, you just have to do exactly what everyone else has to do: launch your app again, requesting elevation. There are several ways to launch elevated, the 'run as' verb probably being the easiest.

所以,我寻求帮助如何在 Java 中使用 'run as' 动词和 ShellExecute 的解决方案。

好的,这是对我问题的回答。我将把它留在这里以供将来参考。首先,我从 here 下载了 JNA。 运行 它们与 java -jar jna.jar 一起导入到我的 Eclipse 项目中。

如果您在 UAC 中回答 yes,这将以管理员身份打开 cmd,然后它将 运行 netsh 命令。这是启动 UAC 的 runas。您可以使用 open 以普通用户身份启动 cmd。

This SO answer helped me a lot with jna, a this one too with passing arguments in ShellExecute. Here 你可以阅读 /S/C 的作用。

import com.sun.jna.*;
import com.sun.jna.platform.win32.ShellAPI;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.*;

public class Main {
    public interface Shell32 extends ShellAPI, StdCallLibrary {
        Shell32 INSTANCE = (Shell32)Native.loadLibrary("shell32", Shell32.class);

        WinDef.HINSTANCE ShellExecuteA(WinDef.HWND hwnd,
                                      String lpOperation,
                                      String lpFile,
                                      String lpParameters,
                                      String lpDirectory,
                                      int nShowCmd);
    }

    public static void main(String[] args) {
        WinDef.HWND h = null;
        Shell32.INSTANCE.ShellExecuteA(h, "runas", "cmd.exe", "/S /C \"netsh interface ip set address name=\"Wi-Fi\" static 192.168.88.189 255.255.255.0 192.168.88.1 1\"", null, 1);