2007年8月31日 星期五

Java執行外部指令

如何在Java程裡執行外部的程式,以下以執行linux裡的掃描AP程式iwlist來做說明:

Runtime rt = Runtime.getRuntime();
try {
Process proc = rt.exec(new String[] {"/sbin/iwlist","eth2", "scan" } );
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String str;
while((str = br.readLine()) != null)
System.out.println(str);
}
catch(Exception e){
e.printStackTrace();
}

0 意見: