public class MacAddressFinder {    /**
     * Package access. Find the Mac address of the machine depending on the
     * operating system.
     * <p>
     * @return null if we cannot find out.
     */
    String getMacAddress() {        try {
            if(CommonUtils.isWindows()) {
                return getWindowsMac();
            }
            else if(CommonUtils.isMacOSX()) {
                return getOSXMac();
            }
            else if(CommonUtils.isSolaris()) {
                return getSolarisMac();
            }
            else if(CommonUtils.isLinux()) {
                getLinuxMac();
            }
            else {
                return null;
            }
        } catch (IOException iox) {
              return null;
        }
        return null;
    }    private String getWindowsMac() throws IOException {
        String result = runCommand("ipconfig /all");
        return parseResult(result,":");
    }    private String getOSXMac() throws IOException {
        String result = runCommand("ifconfig -a");
        return parseResult(result,"ether");
    }    private String getLinuxMac() throws IOException {
        String result = runCommand("LANG=C /sbin/ifconfig");
        if(result.length()<17)//unknown result, but it's gotta be bigger than 17
            result = runCommand("LANG=C /bin/ifconfig");
        if(result.length() < 17) //need to try another?
            result = runCommand("LANG=C ifconfig");//getting desperate here.
        return parseResult(result,"hwaddr");
    }    private String getSolarisMac() throws IOException {
        String result = runCommand("ifconfig -a");//TODO1: correct command?
        return parseResult(result,"ether");//TODO1: correct delimiter?
    }    private String parseResult(String result, String delimiter) {
        result = result.toLowerCase();//lets ignore all case
        StringTokenizer tok = new StringTokenizer(result,"\n");
        while(tok.hasMoreTokens()) {//for each line of result
            String line = tok.nextToken();
            int index = line.indexOf(delimiter);
            if(index >= 0) {//the line contains the delimiter
                String address=line.substring(index+delimiter.length()).trim();
                //address contains the rest of the line after the delimiter.
                address = canonicalizeMacAddress(address);
                if(address!=null)
                    return address;//null if in bad form
            }
        }
        return null;
    }
    private String canonicalizeMacAddress(String address) {
        if(address.length()!=17)
            return null;
        //check that we have six pair of numbers, separated by : or -
        StringBuffer ret = new StringBuffer();
        StringTokenizer tok = new StringTokenizer(address,":.-");
        for(int i=0; i<6;i++) {
            String val=null;
            try {
                val = tok.nextToken();
                if(val.length()!=2)
                    return null;
            } catch (NoSuchElementException nsex) {
                return null;
            }
            ret.append(val);
            if(i<5)
                ret.append("-");
        }
        return ret.toString();
    }    /**
     * @return the results of the command we just ran
     * @param command the command - platform dependent.
     */
    private String runCommand(String command) throws IOException {
        //TODO1: make sure the path is set correctly, or we are not going to be
        //able to execute the command
        javax.swing.JOptionPane.showMessageDialog(null,"runCommand");
        Process process=null;
        try{
       process = Runtime.getRuntime().exec(command);
      }
      catch(Exception err)
      {
        javax.swing.JOptionPane.showMessageDialog(null,err.getMessage());      }        InputStream iStream = new BufferedInputStream(process.getInputStream());
        StringBuffer buffer = new StringBuffer();//store the resutls
        javax.swing.JOptionPane.showMessageDialog(null,"iStream");
        while(true) {
            int c = iStream.read();
            if(c==-1) //eof?
                break;
            buffer.append((char)c);
        }//buffer has all the data from the command.
        iStream.close();
        return buffer.toString();
    }
//    public static void main(String[] args) {
        //MacAddressFinder f = new MacAddressFinder();
        //System.out.println("The mac address is "+f.getMacAddress());
//    }
}

解决方案 »

  1.   

    asdmusic8(asdmusic8):
    if(CommonUtils.isWindows()) {
                    return getWindowsMac();中的CommonUtils是什么,找不到呀
      

  2.   

    import java.sql.*;
    import java.util.*;
    //import java.awt.*;
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    //import java.math.*;
    //import java.utils.*;public class MacAddressFinder {    /**
         * Package access. Find the Mac address of the machine depending on the
         * operating system.
         * <p>
         * @return null if we cannot find out.
         */
        String getMacAddress() {        try {
                //if(CommonUtils.isWindows()) {
                    return getWindowsMac();
    /*            }
                else if(CommonUtils.isMacOSX()) {
                    return getOSXMac();
                }
                else if(CommonUtils.isSolaris()) {
                    return getSolarisMac();
                }
                else if(CommonUtils.isLinux()) {
                    getLinuxMac();
                }
                else {
                    return null;
                }
    */
            } catch (IOException iox) {
                  return null;
            }        //return null;
        }    private String getWindowsMac() throws IOException {
            String result = runCommand("ipconfig /all");
            return parseResult(result,":");
        }    private String getOSXMac() throws IOException {
            String result = runCommand("ifconfig -a");
            return parseResult(result,"ether");
        }    private String getLinuxMac() throws IOException {
            String result = runCommand("LANG=C /sbin/ifconfig");
            if(result.length()<17)//unknown result, but it's gotta be bigger than 17
                result = runCommand("LANG=C /bin/ifconfig");
            if(result.length() < 17) //need to try another?
                result = runCommand("LANG=C ifconfig");//getting desperate here.
            return parseResult(result,"hwaddr");
        }    private String getSolarisMac() throws IOException {
            String result = runCommand("ifconfig -a");//TODO1: correct command?
            return parseResult(result,"ether");//TODO1: correct delimiter?
        }    private String parseResult(String result, String delimiter) {
            result = result.toLowerCase();//lets ignore all case
            StringTokenizer tok = new StringTokenizer(result,"\n");
            while(tok.hasMoreTokens()) {//for each line of result
                String line = tok.nextToken();
                int index = line.indexOf(delimiter);
                if(index >= 0) {//the line contains the delimiter
                    String address=line.substring(index+delimiter.length()).trim();
                    //address contains the rest of the line after the delimiter.
                    address = canonicalizeMacAddress(address);
                    if(address!=null)
                        return address;//null if in bad form
                }
            }
            return null;
        }
        private String canonicalizeMacAddress(String address) {
            if(address.length()!=17)
                return null;
            //check that we have six pair of numbers, separated by : or -
            StringBuffer ret = new StringBuffer();
            StringTokenizer tok = new StringTokenizer(address,":.-");
            for(int i=0; i<6;i++) {
                String val=null;
                try {
                    val = tok.nextToken();
                    if(val.length()!=2)
                        return null;
                } catch (NoSuchElementException nsex) {
                    return null;
                }
                ret.append(val);
                if(i<5)
                    ret.append("-");
            }
            return ret.toString();
        }    /**
         * @return the results of the command we just ran
         * @param command the command - platform dependent.
         */
        private String runCommand(String command) throws IOException {
            //TODO1: make sure the path is set correctly, or we are not going to be
            //able to execute the command
            javax.swing.JOptionPane.showMessageDialog(null,"runCommand");
            Process process=null;
            try{
           process = Runtime.getRuntime().exec(command);
          }
          catch(Exception err)
          {
            javax.swing.JOptionPane.showMessageDialog(null,err.getMessage());      }        InputStream iStream = new BufferedInputStream(process.getInputStream());
            StringBuffer buffer = new StringBuffer();//store the resutls
            javax.swing.JOptionPane.showMessageDialog(null,"iStream");
            while(true) {
                int c = iStream.read();
                if(c==-1) //eof?
                    break;
                buffer.append((char)c);
            }//buffer has all the data from the command.
            iStream.close();
            return buffer.toString();
        }
       public static void main(String[] args) {
           MacAddressFinder f = new MacAddressFinder();
            System.out.println("The mac address is "+f.getMacAddress());
       }
    }
      

  3.   

    上面的可以获得windows的MAC的地址!~
    接分!
      

  4.   

    tod204(八十年代),你取的方法不对吧?
    你现在取的是Physical Address而不是MAC
      

  5.   

    不过,这还是不是用  java求得mac,
    二是使用了java对字符串的过滤和分析。