2017-10-18 1 views
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'를 시도하십시오.

답변

3

ProcessBuilder에는 별도의 문자열로 명령 이름과 각 인수를 입력해야합니다. 당신은 (간접적으로) 단일 인수 label -d mylabel123와 명령 p4을 실행하는 프로세스를 구축하고

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

수행 할 때. 당신은 별도의 세 가지 인수와 함께 해당 명령을 실행하는 대신 원하는 :

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

귀하의 단서가 p4 명령에 의해 방출되는 두 번째 경우에 오류 메시지가 (이 "정보를 원하시면 'P4 도움말을'시도"라는 것을했을 것이다). 분명히, 문제는 논쟁과 관련이 있습니다. 하지만 p4은 그 인수 중 하나를 "명령"이라고 부름으로써 약간의 혼란을 야기합니다.

관련 문제