【问题标题】:How to request elevation with the 'run as' verb in Java如何在 Java 中使用“运行方式”动词请求提升
【发布时间】:2018-10-21 20:28:30
【问题描述】:

我尝试运行这个 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)

如何再次启动我的应用程序,使用“运行方式”动词请求提升?这就是我在 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 上面写着:

因此,在 Java 世界中,您只需要完全按照其他人的方式行事 必须做的:再次启动您的应用程序,请求提升。有 启动提升的几种方法,“运行方式”动词可能是 最简单的。

所以,我请求帮助,如何在 Java 中使用带有“run as”动词和 ShellExecute 的解决方案。

【问题讨论】:

  • 如果您知道它已经被问过并且您查看了现有问题,那么请告诉我们您到底在哪里遇到了问题。我们不应该为您编码。副本确实告诉您需要知道的内容。试试看,如果你仍然有问题,请特别提出。
  • 不,副本并不能说明答案。我无法使用“以管理员身份运行”创建快捷方式或右键单击。它必须是双击,然后单击“是”到 UAC。没有答案。
  • @HrvojeT 在重复线程中接受的答案之外有可能的解决方案。如果这对您没有帮助,也许您应该考虑使用诸如 launch4j 之类的东西:stackoverflow.com/questions/19037339/…
  • 不,没有。我需要在 ShellExecute 中使用“运行方式”的解决方案。

标签: java uac


【解决方案1】:

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

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

This SO 答案对我有很大帮助,jnathis 在 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);

【讨论】:

    猜你喜欢
    • 2014-11-11
    • 2018-11-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2011-04-13
    • 2011-08-31
    • 2011-10-30
    • 2014-11-21
    相关资源
    最近更新 更多