如何使用参数执行命令?

问题描述:

如何使用参数在Java中执行命令?如何使用参数执行命令?

Process p = Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php -m 2"}); 

Does'n工作。

String[] options = new String[]{"option1", "option2"}; 
Runtime.getRuntime().exec("command", options); 

也没有工作,因为它没有指定“m”参数。

见,如果这个工程(抱歉,不能测试它现在)

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"}); 

以下应该工作正常。

Process p = Runtime.getRuntime().exec("php /var/www/script.php -m 2"); 

使用ProcessBuilder而不是Runtime#exec()

ProcessBuilder pb = new ProcessBuilder("php", "/var/www/script.php", "-m 2"); 
Process p = pb.start(); 
+0

没有测试过,BTW。 –

+1

不知道ProcessBuilder。感谢分享。 –

+1

如果这样不起作用:'new ProcessBuilder(“php”,“/var/www/script.php”,“-m”,“2”);'' –