Per eseguire un file esterno, che può essere un exe, un bat per windows, o un .app per MacOSX è molto semplice. La classe che utilizzeremo è Runtime che fa parte del package java.io.
Eccone un esempio.
import java.io.*;
public class Prova {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
Process pp=run.exec("c:\\jdk1.3\\bin\\javac");
BufferedReader in =new BufferedReader(new InputStreamReader(pp.getErrorStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
int exitVal = pp.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Ultimi Commenti