java - 使用 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 应用程序运行时。它不要求管理员凭据。
解决方案
推荐阅读
- php - 使用 gitlab-ci.yml 构建 PHP 镜像
- json - 如何使用 jq 重新格式化 json 中的特定数据
- javascript - javascript 跨域请求被阻止:
- excel - 将宏添加到加载项选项卡自定义工具栏?
- tensorflow - 我在共享图层吗?- keras 网络建模
- javascript - 如何创建 sequelize 事务以保存 deeplu 嵌套对象
- java - 为什么 Spring Boot 中的 Keycloak 允许(预期未经授权的)用户?
- javascript - 提取原始 CSS/SCSS 文件并在 JS 中使用输出
- flutter - 如何将容器垂直拉伸到最大屏幕?
- assembly - 为什么我们不能将 A000H 或更高的值移动到 16 位寄存器中?