从 java 代码在 MAC OS 上运行 .pkg

发布于 2024-10-21 13:11:39 字数 684 浏览 2 评论 0原文

我正在尝试从我的 java 代码运行 .mpkg 应用程序:

public void runNewPkg(){

try {

           String command = "sudo installer -pkg Snip.mpkg -target /Applications";
            Process p = Runtime.getRuntime().exec(command);
            System.out.println(p.getErrorStream());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

并且出现以下错误,并且我的终端窗口挂起。

java.lang.UNIXProcess$DeferredCloseInputStream@2747ee05
Password:
Sumit-Ghoshs-iMac-3:downloads sumitghosh3$ Password:
Password:
-bash: **********: command not found

Sumit-Ghoshs-iMac-3:downloads sumitghosh3$
  • 我认为我还需要提供密码才能从命令行运行 pkg 你能告诉我该怎么做吗?

Am trying to run a .mpkg application from my java code :

public void runNewPkg(){

try {

           String command = "sudo installer -pkg Snip.mpkg -target /Applications";
            Process p = Runtime.getRuntime().exec(command);
            System.out.println(p.getErrorStream());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

And am getting the following error and my terminal window hangs..

java.lang.UNIXProcess$DeferredCloseInputStream@2747ee05
Password:
Sumit-Ghoshs-iMac-3:downloads sumitghosh3$ Password:
Password:
-bash: **********: command not found

Sumit-Ghoshs-iMac-3:downloads sumitghosh3$
  • I Think i need to provide the password also to run the pkg from the command line
    Could you tell me how i can do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-10-28 13:11:39

您可以向 sudo 提供密码:

echo "p@sw0rd" | sudo -S cal -y 2011

上面的命令以 root 权限运行“cal -y 2011”。

You can provide the password to sudo:

echo "p@sw0rd" | sudo -S cal -y 2011

The command above runs 'cal -y 2011' with root permissions.

源来凯始玺欢你 2024-10-28 13:11:39

我实际上会尝试编辑 /etc/sudoers 文件以不提示输入密码。如果您使用 NOPASSWD 标签,您应该能够做到这一点。一个示例条目是:

sumitghosh3 ALL=(ALL) NOPASSWD: ALL

I would actually try editing your /etc/sudoers file to not prompt for a password. If you use the NOPASSWD tag, you should be able to do that. An example entry would be:

sumitghosh3 ALL=(ALL) NOPASSWD: ALL
小镇女孩 2024-10-28 13:11:39

如果您想要一个用于提升权限的交互式解决方案,我已使用 openscript 来提升包装的 shell 脚本的权限。它是这样的:

import java.io.File;
import java.text.MessageFormat;

/**
 * OsxExecutor.java
 */
public class OsxExecutor {

    private String error = null;
    private String output = null;

    /**
     * Privileged script template format string.
     * Format Arguments:
     * <ul>
     * <li> 0 = command
     * <li> 1 = optional with clause
     * </ul>
     */
    private final static String APPLESCRIPT_TEMPLATE = 
        "osascript -e ''try''"
        + " -e ''do shell script \"{0}\" {1}''" 
        + " -e ''return \"Success\"''" 
        + " -e ''on error the error_message number the error_number'' "
        + " -e ''return \"Error: \" & error_message''"
        + " -e ''end try'';";


    public void executeCommand(String command, boolean withPriviledge) {
        String script = MessageFormat.format(APPLESCRIPT_TEMPLATE,
                                             command,
                                             withPriviledge
                                              ?  "with administrator privileges"
                                               : "");
        File scriptFile = null;
        try {
            scriptFile = createTmpScript(script);
            if (scriptFile == null) {
                return;
            }
            // run script
            Process p = Runtime.getRuntime().exec(scriptFile.getAbsolutePath());

            StreamReader outputReader = new StreamReader(p.getInputStream());
            outputReader.start();
            StreamReader errorReader = new StreamReader(p.getErrorStream());
            errorReader.start();

            int result = p.waitFor();

            this.output = outputReader.getString();
            if (result != 0) {
                this.error = "Unable to run script " 
                    + (withPriviledge ? "with administrator privileges" : "") 
                    + "\n" + script + "\n"
                        + "Failed with exit code: " + result
                        + "\nError output: " + errorReader.getString();
                return;
            }
        } catch (Throwable e) {
            this.error = "Unable to run script:\n" + script
                    + "\nScript execution "
                    + (withPriviledge ? " with administrator privileges" : "") 
                    + " failed: " + e.getMessage();
        } finally {
            if (scriptFile.exists()) {
                scriptFile.delete();
            }
        }
    }
}

如果 withPrivilege 标志为 true,则会出现一个密码对话框。未显示的是 createTmpScript(),它在 /tmp 中创建可执行文件,以及 StreamReader,它扩展了 Thread 并且是用于捕获 stdoutstderr 流。

If you want an interactive solution for elevating privilege, I have used openscript to elevate privilege of a wrapped shell script. It goes something like this:

import java.io.File;
import java.text.MessageFormat;

/**
 * OsxExecutor.java
 */
public class OsxExecutor {

    private String error = null;
    private String output = null;

    /**
     * Privileged script template format string.
     * Format Arguments:
     * <ul>
     * <li> 0 = command
     * <li> 1 = optional with clause
     * </ul>
     */
    private final static String APPLESCRIPT_TEMPLATE = 
        "osascript -e ''try''"
        + " -e ''do shell script \"{0}\" {1}''" 
        + " -e ''return \"Success\"''" 
        + " -e ''on error the error_message number the error_number'' "
        + " -e ''return \"Error: \" & error_message''"
        + " -e ''end try'';";


    public void executeCommand(String command, boolean withPriviledge) {
        String script = MessageFormat.format(APPLESCRIPT_TEMPLATE,
                                             command,
                                             withPriviledge
                                              ?  "with administrator privileges"
                                               : "");
        File scriptFile = null;
        try {
            scriptFile = createTmpScript(script);
            if (scriptFile == null) {
                return;
            }
            // run script
            Process p = Runtime.getRuntime().exec(scriptFile.getAbsolutePath());

            StreamReader outputReader = new StreamReader(p.getInputStream());
            outputReader.start();
            StreamReader errorReader = new StreamReader(p.getErrorStream());
            errorReader.start();

            int result = p.waitFor();

            this.output = outputReader.getString();
            if (result != 0) {
                this.error = "Unable to run script " 
                    + (withPriviledge ? "with administrator privileges" : "") 
                    + "\n" + script + "\n"
                        + "Failed with exit code: " + result
                        + "\nError output: " + errorReader.getString();
                return;
            }
        } catch (Throwable e) {
            this.error = "Unable to run script:\n" + script
                    + "\nScript execution "
                    + (withPriviledge ? " with administrator privileges" : "") 
                    + " failed: " + e.getMessage();
        } finally {
            if (scriptFile.exists()) {
                scriptFile.delete();
            }
        }
    }
}

If withPriviledge flag is true, a password dialog will be raised. Not shown is createTmpScript() which creates an executable file in /tmp, and StreamReader which extends Thread and is used to capture both stdout and stderr streams.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文