如何使用参数在Java中执行命令?
Process p = Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php -m 2"});
不行吗?
String[] options = new String[]{"option1", "option2"};
Runtime.getRuntime().exec("command", options);
它是否也有效,因为它没有指定“m”参数。
答案 0 :(得分:22)
使用ProcessBuilder
代替Runtime#exec()
。
ProcessBuilder pb = new ProcessBuilder("php", "/var/www/script.php", "-m 2");
Process p = pb.start();
答案 1 :(得分:22)
看看这是否有效(抱歉现在无法测试)
Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});
答案 2 :(得分:1)
以下内容应该可以正常使用。
Process p = Runtime.getRuntime().exec("php /var/www/script.php -m 2");