Home >Java >javaTutorial >How to Execute Commands with Parameters in Java?
Executing Commands with Parameters in Java
Executing commands from Java can be a useful technique, especially when integrating with external applications or scripts. In this case, the question explores how to execute a command with specific parameters.
The provided code snippets illustrate attempts to execute PHP commands with parameters, but the attempts were unsuccessful.
To properly execute a command with parameters, the parameters should be included as separate strings in the command array passed to exec(). For example, the following code will execute the PHP command with the "-m" parameter set to "2":
Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});
This approach ensures that the parameters are correctly parsed and passed to the underlying command.
The above is the detailed content of How to Execute Commands with Parameters in Java?. For more information, please follow other related articles on the PHP Chinese website!