martedì 9 ottobre 2012

How to run Windows commands in JAVA and return the result text as a string

http://stackoverflow.com/questions/8963413/how-to-run-windows-commands-in-java-and-return-the-result-text-as-a-string

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=reader.readLine(); 
    while(line!=null) 
    { 
    System.out.println(line); 
    line=reader.readLine(); 
    } 

    } 
    catch(IOException e1) {} 
    catch(InterruptedException e2) {} 

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

Nessun commento: