2012-02-21 9 views
2

Runtime.getRunTime.exec()を使用して複数の入力を送受信する方法を教えてください。Java Runtime.getRunTime.exec()を使用した対話型コマンド

たとえば、openSSLのようなものを実行してcsrを生成したい場合は、州、市区町村、一般名などのようなものを要求します。

Process p = Runtime.getRuntime().exec(cmd); 
OutputStream out = p.getOutputStream(); 
//print stuff p.getInputStream(); 
//Now i want to send some inputs 
out.write("test".getBytes()); 
//flush and close??? don't know what to do here 
//print what ever is returned 
//Now i want to send some more inputs 
out.write("test2".getBytes()); 
//print what ever is returned.. and so on until this is complete 

は、なぜあなたは()に応じてデータを送信するために使用してout.write ながら送信するために必要なものを読むためにp.getInputStream()を使用していません。

Process p = Runtime.getRuntime().exec(cmd); 
OutputStream out = p.getOutputStream(); 
//print stuff p.getInputStream(); 
out.write("test".getBytes()); 
out.close(); //if i don't close, it will just sit there 
//print stuff p.getInputStream(); 
out.write("test".getBytes()); // I can no longer write at this point, maybe because the outputstream was closed? 

答えて

1

なぜあなたはそれに応じてデータを送信するためにout.write()を使用しながら、送信するために必要なものを読むためにp.getInputStream().read()を使用していません。私がやろうとしていますものですが、書き込みが正常に動作していないようにそれはそうhttp://www.rgagnon.com/javadetails/java-0014.html

String line; 
OutputStream stdin = null; 
InputStream stderr = null; 
InputStream stdout = null; 

    // launch EXE and grab stdin/stdout and stderr 
    Process process = Runtime.getRuntime().exec ("/folder/exec.exe"); 
    stdin = process.getOutputStream(); 
    stderr = process.getErrorStream(); 
    stdout = process.getInputStream(); 

    // "write" the parms into stdin 
    line = "param1" + "\n"; 
    stdin.write(line.getBytes()); 
    stdin.flush(); 

    line = "param2" + "\n"; 
    stdin.write(line.getBytes()); 
    stdin.flush(); 

    line = "param3" + "\n"; 
    stdin.write(line.getBytes()); 
    stdin.flush(); 

    stdin.close(); 

    // clean up if any output in stdout 
    BufferedReader brCleanUp = 
    new BufferedReader (new InputStreamReader (stdout)); 
    while ((line = brCleanUp.readLine()) != null) { 
    //System.out.println ("[Stdout] " + line); 
    } 
    brCleanUp.close(); 

    // clean up if any output in stderr 
    brCleanUp = 
    new BufferedReader (new InputStreamReader (stderr)); 
    while ((line = brCleanUp.readLine()) != null) { 
    //System.out.println ("[Stderr] " + line); 
    } 
    brCleanUp.close(); 
+0

:ここ

はから取られた例です。私はもう少し説明して質問を更新します。 – boyco

+1

フラッシュすることを忘れないでください:) – james

+1

これはうまく動作しますが、以前はフラッシュで試してみましたが、私は非常に多くの異なるものを試していました。ありがとう! – boyco

関連する問題