2012-01-22 7 views
11

Eventuali duplicati:
Get output from a process
Executing DOS commands from JavaCome per eseguire i comandi di Windows in JAVA e restituire il testo risultato come una stringa

Sto cercando di eseguire un comando cmd dall'interno di un Programma console JAVA es .:

ver 

e quindi restituisce l'output del comando in una stringa in JAVA, ad es. Uscita:

string result = "Windows NT 5.1" 
+0

Perché il downvote? – Mike

+3

Avresti dovuto cercare questo prima di fare questa domanda. Quando cerco [esegui comandi Windows in JAVA] (http://stackoverflow.com/search?q=run%20Windows%20commands%20in%20JAVA) in cerca di stackoverflow, ho ottenuto risultati inferiori ... http: // stackoverflow.com/questions/2935326/java-library-api-to-help-run-windows-commands http://stackoverflow.com/questions/4031390/executing-dos-commands-from-java http: // stackoverflow. it/questions/7112259/how-to-execute-windows-commands-using-java-change-network-settings –

+0

Questo link ti aiuterà: https://www.codepuran.com/java/execute-dos-command- java/ –

risposta

27

È possibile utilizzare il seguente codice per questo

import java.io.*; 

    public class doscmd 
    { 
     public static void main(String args[]) 
     { 
      try 
      { 
       Process p=Runtime.getRuntime().exec("cmd /c dir"); 
       p.waitFor(); 
       BufferedReader reader=new BufferedReader(
        new InputStreamReader(p.getInputStream()) 
       ); 
       String line; 
       while((line = reader.readLine()) != null) 
       { 
        System.out.println(line); 
       } 

      } 
      catch(IOException e1) {e1.printStackTrace();} 
      catch(InterruptedException e2) {e2.printStackTrace();} 

      System.out.println("Done"); 
     } 
    } 
2

Si può fare qualcosa di simile:

String line; 
Process p = Runtime.getRuntime().exec ("ver"); 
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream())); 
BufferedReader error =new BufferedReader(new InputStreamReader(p.getErrorStream())); 

System.out.println("OUTPUT"); 
while ((line = input.readLine()) != null) 
    System.out.println(line); 
input.close(); 

System.out.println("ERROR"); 
while ((line = error.readLine()) != null) 
    System.out.println(line); 
error.close(); 

Su commento del @RanRag, il problema principale è Windows rispetto a Unix/Mac .

  • WINDOWS: exec ("cmd/c ver");
  • UNIX FLAVOR: exec ("ver");
+0

È necessario chiamare exec con 'Process p = Runtime.getRuntime(). exec (" cmd/C ver ");'. – RanRag

+1

Rifletto che modifica..tx – havexz

5

È possibile utilizzare Runtime exec in java per eseguire i comandi dos dal codice java.

Process p = Runtime.getRuntime().exec("cmd /C ver"); 
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()),8*1024); 

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); 

// read the output from the command 

String s = null; 
System.out.println("Here is the standard output of the command:\n"); 
while ((s = stdInput.readLine()) != null) 
System.out.println(s.replace("[","").replace("]","")); 

uscita = Microsoft Windows Version 6.1.7600

+0

+1 per soln specifico per Windows ... – havexz

Problemi correlati