Having spaces in Runtime.getRuntime().exec with 2 executables

Each argument you pass to the command should be a separate String element. So you command array should look more like… String[] a = new String[] { “C:\path\that has\spaces\plink”, “-arg1”, “foo”, “-arg2”, “bar”, “path/on/remote/machine/iperf -arg3 hello -arg4 world”}; Each element will now appear as a individual element in the programs args variable I would also, … Read more

Using Quotes within getRuntime().exec

Use this: Runtime.getRuntime().exec(new String[] {“sh”, “-l”, “-c”, “./foo”}); Main point: don’t put the double quotes in. That’s only used when writing a command-line in the shell! e.g., echo “Hello, world!” (as typed in the shell) gets translated to: Runtime.getRuntime().exec(new String[] {“echo”, “Hello, world!”}); (Just forget for the moment that the shell normally has a builtin … Read more

Runtime.getRuntime().exec()

Are you familiar with the exec double-quotes bug? (for Runtime.exec or ProcessBuilder) You can try: Runtime.getRuntime().exec(new String[] { “\”C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump\””, “-h”, hostName+user+databaseName}); Just make sure none of your parameters you will have to pass contains double quotes (while not beginning with double quotes) (see bug 6511002) Any parameter like: mykey=”my value with space” … Read more

Running Bash commands in Java

You start a new process with Runtime.exec(command). Each process has a working directory. This is normally the directory in which the parent process was started, but you can change the directory in which your process is started. I would recommend to use ProcessBuilder ProcessBuilder pb = new ProcessBuilder(“ls”); pb.inheritIO(); pb.directory(new File(“bin”)); pb.start(); If you want … Read more