0
好吧,所以要刪除一個Perfoce標籤,CommandLine命令是:p4 label -d mylabel123
。 現在我想用Java來執行這個命令。我試過Runtime.exec()
,它的作用像魅力。但是,當我使用ProcessBuilder
運行相同的命令時,它不起作用。任何幫助讚賞。爲什麼Java Runtime.exec命令正常工作,但ProcessBuilder無法執行Perforce客戶端命令?
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()輸出:標籤mylabel123刪除。
方法exec2()輸出:未知命令。試試'p4 help'獲取信息。