Process p;
String cmd = "rsync --timeout=20 -v -r -E -e \"ssh -o StrictHostKeyChecking=no -i "
+ "/usr/local/my.pem\""
+ " root@<IP>:/usr/local/test/ /other/test";
try {
p = Runtime.getRuntime().exec(cmd);
System.out.println("going to exec1");
int val = p.waitFor();
}
When i try the above code, rsync
does not work and val
= 1
.
while if i try cmd
value directly on ternimal
it works fine.
What is wrong with the code ?
Edited
String[] cmd = new String[]{"rsync", "--timeout=20", "-v", "-r", "-a", "-E", "-e",
"\"ssh -o StrictHostKeyChecking=no -i " + "/usr/local/my.pem" + "\"",
"root" + "@" + "198.168.1.3" + ":" + "/usr/local/test1" ,
"/usr/local" + "/" + "test1"};
try {
p = Runtime.getRuntime().exec(cmd);
System.out.println("going to exec1");
int val = p.waitFor();
}
This time val
= 12
Now what can be wrong
You need to use a different version of exec
, as the one you're using assumes that the command name is the whole string.
You need to use this flavor of exec
, which takes a string array, where each element in the array is one of the parameters.
If fact, this question is a duplicate of How to execute command with parameters? (where you can see an example)