2010-03-17 10 views
7

Je travaille sur une application et j'ai un problème avec la commande shell exécutée depuis l'application java. voici le code:Exécuter la commande shell à partir de Java

public String execRuntime(String cmd) { 

     Process proc = null; 
     int inBuffer, errBuffer; 
     int result = 0; 
     StringBuffer outputReport = new StringBuffer(); 
     StringBuffer errorBuffer = new StringBuffer(); 

     try { 
      proc = Runtime.getRuntime().exec(cmd); 
     } catch (IOException e) { 
      return ""; 
     } 
     try { 
      response.status = 1; 
      result = proc.waitFor(); 
     } catch (InterruptedException e) { 
      return ""; 
     } 
     if (proc != null && null != proc.getInputStream()) { 
      InputStream is = proc.getInputStream(); 
      InputStream es = proc.getErrorStream(); 
      OutputStream os = proc.getOutputStream(); 

      try { 
       while ((inBuffer = is.read()) != -1) { 
        outputReport.append((char) inBuffer); 
       } 

       while ((errBuffer = es.read()) != -1) { 
        errorBuffer.append((char) errBuffer); 
       } 

      } catch (IOException e) { 
       return ""; 
      } 
      try { 
       is.close(); 
       is = null; 
       es.close(); 
       es = null; 
       os.close(); 
       os = null; 
      } catch (IOException e) { 
       return ""; 
      } 

      proc.destroy(); 
      proc = null; 
     } 


     if (errorBuffer.length() > 0) { 
      logger 
        .error("could not finish execution because of error(s)."); 
      logger.error("*** Error : " + errorBuffer.toString()); 
      return ""; 
     } 


     return outputReport.toString(); 
    } 

mais lorsque je tente de exec commande comme:

/export/home/test/myapp -T "some argument" 

monapp lit "some argument" que deux separes arguments.but Je veux lire "some argument" comme seul argument. lorsque j'exécute directement cette commande depuis le terminal, elle est exécutée avec succès. J'ai essayé '"some argument"', ""some argument"", "some\ argument" mais cela n'a pas fonctionné pour moi. comment puis-je lire cet argument comme un argument.

Répondre

16

Je rappelle que la surcharge de la méthode exec fournit un paramètre pour les arguments séparément. Vous devez utiliser cela

Yup. Voici ce

public Process exec(String[] cmdarray) 
      throws IOException 

Assurez la ligne de commande et tous les arguments éléments Seperate du tableau String

+0

thx @midhat. Cela fonctionne pour moi. – Aykut

+0

[Quand Runtime.exec() 'ne va pas: Naviguer autour des pièges liés à la méthode' Runtime.exec() '] (http://www.javaworld.com/article/2071275/core-java/fr -runtime-exec --- won-t.html) ou peut-être un avenir [requête de moteur de recherche] (https://duckduckgo.com/?q=When+Runtime.exec+wont+Navigate+yourself+around+ pièges + liés + à + la méthode + Runtime.exec +). –

-3

d'abord une chaîne
chaîne cmd = "/ export/home/test/myapp -T \" un argument \ "";
puis exécutez cmd dans proc

+2

Cela ne fait absolument rien d'autre que ce qu'Aykut a déjà signalé. –

+0

@MiladNaseri Sauf qu'il échappe correctement les citations qui était l'erreur réelle d'Aykut. – Silveri

Questions connexes