【发布时间】:2025-11-30 06:35:01
【问题描述】:
我正在尝试编写一个 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 应用程序运行时。它不要求管理员凭据。
【问题讨论】:
-
那么实际发生了什么?我希望这只是挂起......
-
是的,它只是挂起。点击按钮后。有没有办法让 gui 弹出窗口在处理安装之前询问管理员凭据?
-
我认为您需要 1. 在启动进程之前提示用户输入密码,2. 使用
-S选项到sudo,以及 3. 将密码发送到process's standard input -
您可能还应该在后台线程中运行该进程。
标签: java macos javafx terminal jar