这也叫简单问题
碰到我算你运气/**
 * MacAddress.java.
 *
 */
import java.io.*;/**
 *获得本机MAC地址.
 *
 *通过运行'ipconfig /all' Windows系统命令获得。
 */
public  class  MacAddress
{
  public static void main(String[] arg)
  {
    String macaddress ;
    MacAddress mac = new MacAddress();
    System.out.println(mac.getMac());
    
  }  
  
  /**
   *取得本机 MAC Address.
   */
  public static String getMac()
  {
    String mac;
    String netdata="";
    try
    {
      java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");
      InputStream istr = proc.getInputStream();
      byte[] data = new byte[1024];
      istr.read(data);
      netdata = new String(data);     
    }
    catch(IOException e)
    {
      System.out.println("error="+e);
    }
    return mac = procAll(netdata);
  }
  
  /*
   *处理 ipconfig /all 返回的字符串.
   *@param str  'ipconfig /all' 返回的字符串
   */
  private static String procAll(String str)
  {
    return procStringEnd(procFirstMac(procAddress(str)));
  }
  
  /*
   *去掉 MAC Address 之前的字符.
   *@param str 'ipconfig /all' 返回的字符串
   */
  private static String procAddress(String str)
  {
    int indexof = str.indexOf("Physical Address");
    if(indexof>0)
    return str.substring(indexof,str.length());
    return str;
  }
  
  /*
   *获得 MAC Address 的开始位置.
   *@param str procAddress 返回的字符串
   */
  private static String procFirstMac(String str)
  {
    int indexof = str.indexOf(":");
    if(indexof>0)
    return str.substring(indexof+1,str.length()).trim();
    return str;
  }
  
  /*
   *获得 MAC Address 的结束位置.
   *@param str procFirstMac 返回的字符串
   */
  private static String procStringEnd(String str)
  {
    int indexof = str.indexOf("\r");
    if(indexof>0)
    return str.substring(0,indexof).trim();
    return str;
  }
}

解决方案 »

  1.   

    InetAddress localHost = InetAddress.getLocalHost();
    System.out.println(localHost.getHostName());
    System.out.println(localHost.getHostAddress());
    它那个不安全,直接用java的!
      

  2.   

    如果你有多个ip
    InetAddress localHost = InetAddress.getLocalHost();
    Inet = InetAddress.getAllByName(localHost.getHostName());
    for (int j=1;j<=Inet.length;j++){
    System.out.println(Inet[j-1].toString());
    }
      

  3.   

    楼上,是MAC地址
    再再楼上,你的MacAddress类改名个什么WindowsMacAddress合适点public interface MacAddress
    public class WindowsMacAddress implements MacAddress
      

  4.   

    楼上,人家可是取MAC地址哈。你的方法似乎只能拿到IP哦。