好的,要删除Perfoce标签,CommandLine命令是:p4 label -d mylabel123。现在,我想使用Java来执行这个命令。我试过Runtime.exec(),它很有魅力。但是,当我使用ProcessBuilder运行相同的命令时,它无法工作。任何帮助都很感激。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
exec1("p4 label -d mylabel123");
exec2("p4","label -d mylabel123");
}
public static void exec1(String cmd)
throws java.io.IOException, InterruptedException {
System.out.println("Executing Runtime.exec()");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
proc.waitFor();
}
public static void exec2(String... cmd) throws IOException, InterruptedException{
System.out.println("\n\nExecuting ProcessBuilder.start()");
ProcessBuilder pb = new ProcessBuilder();
pb.inheritIO();
pb.command(cmd);
Process process = pb.start();
process.waitFor();
}
}方法exec1()输出: Label mylabel123已删除。
方法exec2()输出:未知命令。尝试'p4帮助‘获得信息。
发布于 2017-10-18 18:52:52
ProcessBuilder希望您将命令名和每个参数作为单独的字符串提供。当你(间接)执行
pb.command("p4", "label -d mylabel123");您正在构建一个运行带有单个参数的命令p4的进程,label -d mylabel123。相反,您需要使用三个独立的参数运行该命令:
pb.command("p4", "label", "-d", "mylabel123");您的线索应该是,第二种情况下的错误消息是由p4命令发出的(它写着“p4 help‘for info")。显然,问题在于争论。不过,我承认,p4将其参数之一称为“命令”,确实会造成一些混乱。
https://stackoverflow.com/questions/46816827
复制相似问题