0

i want to run a program command with window command prompt. i have to specify the path of the program before i can execute my command. i have seen other SO question but most answer only have command without path.

try {

Runtime rt = Runtime.getRuntime();
String str ="C:/Rsync/rsync -v -e ssh /cygdrive/c/test/from.zip zulkifli@address:/home/zulkifli/test/";   //put path and command

//i put path and command to str string but this will return error 
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
rt.exec("cmd.exe /c start command");

System.out.println(str);

} catch (Exception ex) {}

if we do manually from command prompt we can insert path with cd path/.. and then input the command.

but how does we program it with java? below is the error when i execute the program. the command is legal when i run at cmd

enter image description here

1
  • No need to cd you can invoke rsync using absolute path and use ProcessBuilder for passing in multiple parameters to cmd. Commented Jun 3, 2014 at 9:04

1 Answer 1

1

You could build the process with the ProcessBuilder, don't do cd, don't call cmd.exe.

    String commands = "C:/Rsync/rsync -v -e ssh /cygdrive/c/test/from.zip zulkifli@address:/home/zulkifli/backup_data/";
    String[] commandArray = commands.split("\\s+");
    ProcessBuilder processBuilder = new ProcessBuilder(commandArray);
    Process process = processBuilder.start();
    process.waitFor();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.