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

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});
Answer from Chris Stratton on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exec-method
Java Runtime exec() Method with Examples - GeeksforGeeks
October 23, 2023 - Runtime.getRuntime().exec(cmd); exec(String[] cmdarray, String[] envp) : In this version, you can also specify environment variables (envp) for the process. This is useful for customizing the execution environment. exec(String[] cmdarray, String[] ...
🌐
Stack Overflow
stackoverflow.com › questions › 28394001 › java-runtime-getruntime-exec-to-run-c-executable-file-with-parameters
Java Runtime.getRuntime().exec to run 'c executable file' with parameters - Stack Overflow
String[] cmdUpdateTrain = new String[]{"/bin/bash", "-c", "./bin/svm-train -s 0 -t 0 -d 3 -g 0.0 -r 0.0 -n 0.5 -m 40.0 -c 1.0 -e 0.001 -p 0.1 ./data/trainfile ./model/update.model"}; Runtime.getRuntime().exec(cmdUpdateTrain);
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Runtime.html
Runtime (Java Platform SE 8 )
April 21, 2026 - Executes the specified command and arguments in a separate process with the specified environment and working directory.
🌐
Stack Overflow
stackoverflow.com › questions › 49913817 › using-runtime-getruntime-exec-with-multiple-parameters
java - Using Runtime.getRuntime().exec with multiple parameters - Stack Overflow
The issue is that it returns exit status code of success i,e "0" when invoked through Java, but doesn't performs the updates it is suppose to do (appears like it is not executing). ... opcmsg application='Tester Down 11' object='My Support' severity=minor msg_grp='MyGroup' msg_text='DEV: -m=New Details:Request Detail description' ... String[] command = { "opcmsg", "application=\'Tester Down 11\'", "object=\'My Support\'", "severity=minor", "msg_grp=\'MyGroup\'", "msg_text=\'DEV: -m=New Details:Request Detail description\'" } p = Runtime.getRuntime().exec(command); InputStream stderr = p.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); String errorDescription = null; while ( (errorDescription = br.readLine()) != null) LOGGER.info(errorDescription); exitStatus = p.waitFor(); LOGGER.info("exitStatus : " + exitStatus);
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
When Runtime.exec() won’t | InfoWorld
December 29, 2000 - Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments. In listing 4.6 below, a user tries to execute a command and redirect its output in one call to exec(): ... import java.util.*; import java.io.*; // StreamGobbler omitted for brevity public class BadWinRedirect { public static void main(String args[]) { try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("java jecho 'Hello World' > test.txt"); // any error message?
🌐
More of Less
moreofless.co.uk › system-exec-java-not-working
How to exec external commands in Java with correct parameters
So the above code executes helloworld with three parameters, which are "Mickey and Mouse" and param2. Split it yourself and pass an array of strings to the exec() command instead of getting exec() to do the split. You do not need to add in double-quotes in this method as the exec knows that each array element is a param, regardless of whether it contains spaces or not. var strings = new String[]{"helloworld", "Mickey Mouse", "param2"}; Process process = Runtime.getRuntime().exec(strings);
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Runtime.html
Runtime (Java Platform SE 7 )
Executes the specified command and arguments in a separate process with the specified environment and working directory.
🌐
Real's HowTo
rgagnon.com › javadetails › java-0014.html
Execute an external program - Real's Java How-to
String line; OutputStream stdin = null; InputStream stderr = null; InputStream stdout = null; // launch EXE and grab stdin/stdout and stderr Process process = Runtime.getRuntime ().exec ("/folder/exec.exe"); stdin = process.getOutputStream (); stderr = process.getErrorStream (); stdout = process.getInputStream (); // "write" the parms into stdin line = "param1" + "\n"; stdin.write(line.getBytes() ); stdin.flush(); line = "param2" + "\n"; stdin.write(line.getBytes() ); stdin.flush(); line = "param3" + "\n"; stdin.write(line.getBytes() ); stdin.flush(); stdin.close(); // clean up if any output i
Top answer
1 of 1
3
  1. Use ProcessBuilder of Runtime#exec, it provides a more configurable solution and encourages the use of String[] or List<String> for the commands and parameters, which solves a bunch of issues, especially when your parameters contain spaces.
  2. Read the Process's InputStream

For example...

try {
    String[] command = {"java.exe", "-?"};
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectErrorStream(true);
    Process exec = pb.start();

    BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    String text = null;
    while ((text = br.readLine()) != null) {
        System.out.println(text);
    }

    System.out.println("Process exited with " + exec.waitFor());
} catch (IOException | InterruptedException exp) {
    exp.printStackTrace();
}

Which outputs

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32      use a 32-bit data model if available
    -d64      use a 64-bit data model if available
    -server   to select the "server" VM
                  The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Process exited with 0

This assumes that java.exe is within the PATH environment, otherwise you may be required to provide the full path to it (or change the working directory)

🌐
CodingTechRoom
codingtechroom.com › question › how-to-pass-parameters-to-runtime-getruntime-exec-in-java
How to Pass Parameters to Runtime.getRuntime().exec in Java - CodingTechRoom
String command = "myCommand"; String[] parameters = {"param1", "param2"}; Process process = Runtime.getRuntime().exec(new String[]{command}.add(parameters)); // The command execution can be more safely handled by using ProcessBuilder: ProcessBuilder builder = new ProcessBuilder(command, ...
🌐
Coderanch
coderanch.com › t › 419192 › java › Runtime-getRuntime-exec-String-command
Runtime getRuntime() exec(String command) - How does this work? (Java in General forum at Coderanch)
Example: What I want: above code should run mycmd, and pass the following parameters: -param1 "here is some text" What actually happens: the code runs mycmd, and passes the following parameters: -param1 "here is some text" This causes my external command to barf, or to only process the first word of the quoted parameters. This behavior is documented in Bug # 4365120 (here). This bug was closed without resolution. Does anyone have any idea how I can work around this? ... Hi Phillippe, Welcome to JavaRanch! There's an overloaded version of Runtime.exec() that takes an array of Strings.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › runtime_exec_envp.htm
Java Runtime exec() Method
package com.tutorialspoint; public class RuntimeDemo { public static void main(String[] args) { try { // create a new array of 2 strings String[] cmdArray = new String[2]; // first argument is the program we want to open cmdArray[0] = "notepad.exe"; // second argument is a txt file we want to open with notepad cmdArray[1] = "example.txt"; // print a message System.out.println("Executing notepad.exe and opening example.txt"); // create a process and execute cmdArray and currect environment Process process = Runtime.getRuntime().exec(cmdArray,null); // print another message System.out.println("example.txt should now open."); } catch (Exception ex) { ex.printStackTrace(); } } }
🌐
GitHub
gist.github.com › f3348e7843994bdd56f0
Java Runtime.getRuntime().exec() Example · GitHub
package api; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Exec { /** * @param args */ public static void main(String[] args) { String result = ""; try { // Execute command String command = "k:\\curl.exe -d login_name=mohwtest -d login_passwd=12345 --insecure https://mohw.cyberhood.net.tw/xml_import.php"; Process child = Runtime.getRuntime().exec(command); DataInputStream in = new DataInputStream( child.getInputStream()); BufferedReader br = new BufferedReader( new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { result +=line; } in.close(); } catch (IOException e) { } System.out.println(result); } } Sign up for free to join this conversation on GitHub.