从Java执行killall命令

时间:2014-01-30 15:35:34

标签: java macos kill-process

我必须在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\""});

它不起作用。

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

你走了。

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

答案 1 :(得分:1)

你可以试试这个:

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

Runtime.getRuntime().exec(command);

您还可以尝试以上操作,在Google Chrome中添加引号,如下所示:

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