How to execute command with parameters?

好久不见. 提交于 2019-11-25 23:47:27

问题


How am I to execute a command in Java with parameters?

I\'ve tried

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

which doesn\'t work.

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

This doesn\'t work as well, because the m parameter is not specified.


回答1:


See if this works (sorry can't test it right now)

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



回答2:


Use ProcessBuilder instead of Runtime#exec().

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



回答3:


The following should work fine.

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


来源:https://stackoverflow.com/questions/7134486/how-to-execute-command-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!