【问题标题】:Executing killall command from Java从 Java 执行 killall 命令
【发布时间】:2014-01-30 15:35:33
【问题描述】:

我必须在 Mac OS 上执行命令:

killall -KILL "Google Chrome"

当我在终端中执行它或使用此命令运行 .command 文件时,它可以工作。

我在 Java 代码中都试过了

Runtime.getRuntime().exec("/usr/bin/killall -KILL \"Google Chrome\"");
Runtime.getRuntime().exec("killall -KILL \"Google Chrome\"");
Runtime.getRuntime().exec("/bin/bash -c \"killall -KILL \\\"Google Chrome\\\"\"");
Runtime.getRuntime().exec("bash -c \"killall -KILL \\\"Google Chrome\\\"\"");
Runtime.getRuntime().exec("/usr/bin/killall", new String[]{"-KILL", "Google Chrome"});
Runtime.getRuntime().exec("killall", new String[]{"-KILL", "Google Chrome"});
Runtime.getRuntime().exec("/bin/bash", new String[]{"-c", "killall -KILL \"Google Chrome\""});
Runtime.getRuntime().exec("bash", new String[]{"-c", "killall -KILL \"Google Chrome\""});

而且它不起作用。

可能是什么问题?

【问题讨论】:

    标签: java macos kill-process


    【解决方案1】:

    给你。

    String cmds[] = {"killall","Google Chrome"};
    Runtime.getRuntime().exec(cmds);
    

    【讨论】:

    • 是的,没错。我对 exec(String) 和 exec(String, String[]) 使用了错误的变体,完全错过了正确的变体:exec(String[])。谢谢!
    • @user2780836 如果这是您最喜欢的正确答案,请随时用绿色复选标记将其标记。
    【解决方案2】:

    你可以试试这个:

    String[] command = { "/usr/bin/killall", "-KILL", "Google Chrome" };
    
    Runtime.getRuntime().exec(command);
    

    您也可以尝试上述方法,在 Google Chrome 中添加引号,如下所示:

    String[] command = { "/usr/bin/killall", "-KILL","\"Google Chrome\"" };
    

    【讨论】:

    • 是的,没错。我对 exec(String) 和 exec(String, String[]) 使用了错误的变体,完全错过了正确的变体:exec(String[])。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 2013-08-25
    • 2017-10-07
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多