1

基本的に、ターミナルでこれらのコマンドを手で入力すると、sift プログラムが動作して .key ファイルが書き込まれますが、自分のプログラムから呼び出そうとすると、何も書き込まれません。

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(); 
        } 
}
4

5 に答える 5

4

使用しているコマンドラインは、次の形式のDOSコマンドラインです。

prog < input > output

プログラム自体は引数なしで実行されます。

prog

ただし、コードからのコマンドは次のように実行されます

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

考えられる修正:

a)Javaを使用して入力ファイルと出力ファイルを処理する

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
...

参照:JavaプロセスからInputStreamを読み取れません(Runtime.getRuntime()。exec()またはProcessBuilder)

b)cmd.exeを使用して、コマンドをそのまま実行します

cmd.exe /c "prog < input > output"
于 2012-07-27T18:47:00.590 に答える
1

<リダイレクト (および>) はRuntime.exec、シェルによって解釈および実行されるため、使用できません。1 つの実行可能ファイルとその引数でのみ機能します。

参考文献:

于 2012-07-27T18:44:06.470 に答える
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(...);
于 2012-07-27T18:48:44.997 に答える
0

テストします、大丈夫です。あなたが試すことができます。幸運を

String cmd = "cmd /c siftWin32 <box.pgm>a.key"; 
Process process = Runtime.getRuntime().exec(cmd);
于 2013-05-30T16:13:33.540 に答える