2017-10-18 14 views
0

Perforce Labelを削除するには、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'を試してください。

答えて

3

ProcessBuilderには、コマンド名と各引数を別々の文字列として入力する必要があります。あなたは(間接的に)あなたは、単一の引数、label -d mylabel123でコマンドp4を実行するプロセスを構築している

pb.command("p4", "label -d mylabel123"); 

実行するとき。あなたは3つの別々の引数を指定して、そのコマンドを実行する代わりにしたい:

pb.command("p4", "label", "-d", "mylabel123"); 

あなたの手掛かりがp4コマンドによって放出される第2のケースでエラーメッセージが(それは「情報は 『p4 help』を試してみてください」と言うことだっただろう)。明らかに、問題は議論である。しかし、私はあなたに、p4がその引数の1つを "コマンド"と呼ぶことによって混乱を招くことを認めています。

関連する問題