2017-10-16 18 views
1

私はJavaおよびEXECコマンド - パイプ複数のコマンド

しかしpass値が、その場合には空白になって

pass = executeCommand("/usr/bin/openssl rand -base64 8 | tr -d '+' | cut -c1-8")

のように見えるのコマンドを実行しようとしています。私はそれを残す場合は

pass = executeCommand("/usr/bin/openssl rand -base64 8")

としてパイプではない、それは罰金

方法executeCommand

private static String executeCommand(String command) throws Exception { 

     StringBuffer output = new StringBuffer(); 

     Process p; 
     try { 
     p = Runtime.getRuntime().exec(command); 
     p.waitFor(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 

     String line = ""; 
     while ((line = reader.readLine()) != null) { 
      output.append(line + "\n"); 
     } 

     } 
     catch (Exception e) { 
     e.printStackTrace(); 
     throw new Exception("Could not generate password : " + e.getMessage()); 
     } 

     return output.toString().trim(); 

    } 

任意の提案をどのように動作するバージョンをパイプその取得するように見える作品?

答えて

1

これを試してみてください:

String[] COMPOSED_COMMAND = { 
     "/bin/bash", 
     "-c", 
     "/usr/bin/openssl rand -base64 8 | tr -d '+' | cut -c1-8",}; 
Process p = Runtime.getRuntime().exec(COMPOSED_COMMAND); 
関連する問題