2012-07-27 2 views
1

기본적으로이 명령을 손으로 에 입력하면 sift 프로그램이 작동하고 .key 파일을 작성하지만 내 프로그램에서 호출하려고하면 아무 것도 쓰지 않습니다.java getRuntime(). exec()가 작동하지 않습니까?

나는 exec() 메소드를 올바르게 사용하고 있습니까? API를 살펴본 결과 내가 잘못 된 부분을 발견 할 수없는 것 같습니다. 그들은 해석 쉘에 의해 실행될 때

public static void main(String[] args) throws IOException, InterruptedException 
{   
     //Task 1: create .key file for the input file 
     String[] arr = new String[3]; 
     arr[0] = "\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/siftWin32.exe\""; 
     arr[1] = "<\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/cover_actual.pgm\""; 
     arr[2] = ">\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/keys/cover_actual.key\""; 

     String command = (arr[0]+" "+arr[1]+" "+arr[2]); 

     Process p=Runtime.getRuntime().exec(command); 
     p.waitFor(); 
     BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
     String line=reader.readLine(); 

     while(line!=null) 
     { 
      System.out.println(line); 
      line=reader.readLine(); 
     } 
} 
+0

가 어떤 오류를 ?? –

+0

나는 어떤 에러도 내지 않지만, .key 파일을 다음과 같이 쓰지 않는다. –

+0

'Runtime.exec'에서 출력 리다이렉션을 사용할 수 있습니까? – zneak

답변

4

사용하고있는 명령 줄 형식의 DOS 명령 줄입니다 :

prog <input> output 

프로그램 자체가 인수없이 실행 :

prog 

코드에서 명령이

prog "<" "input" ">" "output" 

로 실행됩니다 그러나 가능한 수정 :

A)를 사용하여 자바 처리하는 입력 및 출력 파일

Process process = Runtime.getRuntime().exec(command); 
OutputStream stdin = process.getOutputStream(); 
InputStream stdout = process.getInputStream(); 

// Start a background thread that writes input file into "stdin" stream 
... 

// Read the results from "stdout" stream 
... 

참조 : Unable to read InputStream from Java Process (Runtime.getRuntime().exec() or ProcessBuilder)

b) cmd.exe를 사용하여 다음과 같이 명령을 실행하십시오.

cmd.exe /c "prog <input> output" 
+0

이 명령은 실제로'Runtime.getRuntime()과 동일합니다.Thumb, 대답을 – Gumbo

+0

끊임없이 표준 입력을 읽고 그것과 동시에 표준 출력을 작성함으로써. 대량의 데이터의 경우 버퍼가 고갈되고 모든 쓰기가 차단됩니다. 올바른 해결책은 두 개의 스레드 또는 비 차단 I/O를 동시에 읽고 쓰는 것입니다. – anttix

0

Runtime.exec과 함께 입출력 방향 재 지정을 사용할 수 없습니다. 반면에 동일한 메소드는 Process 객체를 반환하고 입력 및 출력 스트림에 액세스 할 수 있습니다.

Process process = Runtime.exec("command here"); 

// these methods are terribly ill-named: 
// getOutputStream returns the process's stdin 
// and getInputStream returns the process's stdout 
OutputStream stdin = process.getOutputStream(); 
// write your file in stdin 
stdin.write(...); 

// now read from stdout 
InputStream stdout = process.getInputStream(); 
stdout.read(...); 
+0

업데이트했습니다. 사용중인 명령이 필터처럼 작동하면이 기능이 작동하지 않습니다. exec ("prog", new String [] { "<", "input", ">"output "}) ' – anttix

+0

@anttix, 이것이 내가 사람들에게이 사실을 알기를 기대하는 세부적인 내용입니다. 나는 단지 그 물줄기가 존재한다는 것을 지적하기를 원했다. – zneak

0

나는 괜찮습니다. 당신은 시도 할 수 있습니다. 행운을 빕니다

보통 문제가 발생할 특수 문자
String cmd = "cmd /c siftWin32 <box.pgm>a.key"; 
Process process = Runtime.getRuntime().exec(cmd); 
0

* : 이 코드도 같은 파일 이름이 제대로 작동 : "1 - 1 권 (Fronte) .JPG"

String strArr[] = {"cmd", "/C", file.getCanonicalPath()}; 
Process p = rtObj.exec(strArr);///strCmd); 

도 동의, 여기서 리디렉션은 지원되지 않습니다.

은 Windows 7에서 {guscoder : 912081574} 테스트 당신이 얻을 인해

관련 문제