Perhaps there are significant differences between running exec
on Linux versus Windows, beyond the obvious, so I grabbed a demo, but it also crashes when sent a cmdarray
of "hello world".
What is the correct usage for a "command array" of "echo" commands?
crash:
run:
echo hello world...? cannot open notepad
java.io.IOException: Cannot run program "echo hello world 1": error=2, No such file or directory
BUILD SUCCESSFUL (total time: 0 seconds)
adapted code:
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] = "echo hello world 1";
// second argument is a txt file we want to open with notepad
cmdArray[1] = "echo hello world 2";
// print a message
System.out.println("echo hello world...? cannot open notepad");
// create a process and execute cmdArray
Process process = Runtime.getRuntime().exec(cmdArray);
// print another message
System.out.println("example.txt should now open.");
} catch (Exception ex) {
System.out.println(ex);
}
}
}
see also: sending a cmdarray for exec to process -- hello world