下面这段小程序在Windows 2000上能运行通过(应该在win9x, xp ,2003上也可以)没有试过,而且有多个网卡的情况没有考虑,这里只是把所有的网卡MAC地址罗列出来,按照这个思路,在Linux平台上,作个判断,取"ifconfig"命令的输出同样可以取得网卡MAC地址。
import java.io.*;
public class Cmdoutput{
public static void main(String[] args){
try{
Process process = Runtime.getRuntime().exec ("ipconfig /all");
InputStreamReader ir=new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
String line;
while ((line = input.readLine ()) != null)
if( line.indexOf("Physical Address")>0 ) {
String MACAddr = line.substring(line.indexOf("-")-2);
System.out.println("MAC address = ["+ MACAddr + "]");
}
}catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
}
}
}