[英]Cant run commands With PATH with command prompt using Java?
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. 如果我们在命令提示符下手动进行操作,则可以使用cd path / ..插入路径,然后输入命令。
but how does we program it with java? 但是我们如何用Java编程呢? below is the error when i execute the program.
下面是我执行程序时的错误。 the command is legal when i run at cmd
当我在cmd上运行时,该命令是合法的
You could build the process with the ProcessBuilder, don't do cd
, don't call cmd.exe
. 您可以使用ProcessBuilder来构建进程,不要执行
cd
,不要调用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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.