Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute command with parameters?

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.

like image 879
Alex Avatar asked Aug 20 '11 20:08

Alex


People also ask

How do you pass arguments in a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

What are parameters in commands?

Parameters can be entered in any order. If a parameter has an associated argument, the argument must always follow the parameter. A parameter must start with a '-'; otherwise, it is assumed to be an argument. The maximum length of any single parameter that can be entered into the CLI is 128 bytes.


1 Answers

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

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"}); 
like image 156
Chris Stratton Avatar answered Sep 23 '22 07:09

Chris Stratton