2017-10-16 42 views
1

Je suis en train d'exécuter une commande qui ressemble àcommande Java et exec - multiples tuyaux commandes

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

mais pass valeur est vide dans ce cas. Quand je laisse pas canalisé comme

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

il fonctionne très bien

Méthode executeCommand ressemble

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

    } 

Toute suggestion comment obtenir cette canalisé la version à travailler?

Répondre

1

Essayez ceci:

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