首页 > 解决方案 > 使用 sudo 命令时使用 GUI 使 javafx jar 提示管理员凭据

问题描述

我正在尝试编写一个 javafx 应用程序,它将在 mac 设备中安装另一个 pkg。我已经使用了命令

sudo -v installer -pkg /Downloads/one.pkg -target /

从 javafx 应用程序单击按钮安装 pkg。

public class macinstall extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Sample");
        Button button = new Button("Click");

        EventHandler<ActionEvent> event = ae -> install2();
        button.setOnAction(event);

        HBox hbox = new HBox(button);

        Scene scene = new Scene(hbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void runProcess(String[] Command) {
        ProcessBuilder builderObj = new ProcessBuilder(Command);
        try {
            Process process = builderObj.start();
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public static void install2() {
        String[] installCommand = {"bash", "-c", "sudo -v installer -pkg /Downloads/one.pkg -target /"};
        runProcess(installCommand);
        System.out.println("Install Completed");
    }
}

该程序在我通过终端运行时有效,在终端中询问管理员密码。但是当我使用 jre 作为 javafx 应用程序运行时。它不要求管理员凭据。

标签: javamacosjavafxterminaljar

解决方案


感谢您的建议 James_D。

String[] installCommand = {"osascript","-e","do shell script "installer -pkg /Downloads/one.pkg -target /" 具有管理员权限"};

我使用了上述命令,该命令从 OSX 访问 GUI,在安装 pkg 之前为我提供用户身份验证弹出窗口。

现在用户身份验证的处理如下图所示。

在此处输入图像描述

但我想知道,是否有可能更改图像中弹出的身份验证消息。喜欢而不是“osascript 想要进行更改。输入您的密码以允许这样做”。是否可以配置我的首选字符串或在那里提及我的应用程序名称?


推荐阅读