468x60

mercoledì 13 maggio 2009

Visualizzare l'output della shell in Java

Codice Java per eseguire un comando della shell e visualizzarne l'output:
// start the process command
String []cmd = {executable,param1,..,paramN}
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);

// put a BufferedReader on the process output
InputStream inputstream =
proc.getInputStream();
InputStreamReader inputstreamreader =
new InputStreamReader(inputstream);
BufferedReader bufferedreader =
new BufferedReader(inputstreamreader);

// read the process output
String line;
while ((line = bufferedreader.readLine())
!= null) {
System.out.println(line);
}

// check for process failure
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}

Nessun commento:

Posta un commento

468x60

Cerca su Google

Cerca nel Blog con Google