How to execute command with parameters?

2019-01-01 02:33发布

How to execute command in Java with parameters?

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

Does'n work.

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

Does'n work also, because it doesn't specify the "m" parameter.

3条回答
余欢
2楼-- · 2019-01-01 03:02

Use ProcessBuilder instead of Runtime#exec().

ProcessBuilder pb = new ProcessBuilder("php", "/var/www/script.php", "-m 2");
Process p = pb.start();
查看更多
浮光初槿花落
3楼-- · 2019-01-01 03:12

The following should work fine.

Process p = Runtime.getRuntime().exec("php /var/www/script.php -m 2");
查看更多
情到深处是孤独
4楼-- · 2019-01-01 03:14

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

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});
查看更多
登录 后发表回答