How to get MAC address using Java

Java programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to get MAC address using Java

Post by Neo » Sun Feb 07, 2010 3:49 pm

Not that this not JavaScript. So you need to get this compiled to a Java applet.

Code: Select all

/*
 * Author: Tim Desjardins
 * Copyright (c) 2008 Agwego Enterprises Inc.
 *
 * Feel free to use or abuse this code anyway you wish, without warranty of course. */

     * getMacAddresses - return all mac addresses found
     *
     * @return array of strings (mac addresses) empty if none found
     */
    public static String [] getMacAddresses()
    {
        try {
            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
            
            ArrayList<String> macs = new ArrayList<String>();
            while( nis.hasMoreElements() ) {
                String mac = macToString( nis.nextElement() );
                // not all interface will have a mac address for instance loopback on windows
                if( mac != null ) {
                    macs.add( mac );
                }
            }
            return macs.toArray( new String[0] );
        } catch( SocketException ex ) {
            System.err.println( "SocketException:: " + ex.getMessage() );
            ex.printStackTrace();
        } catch( Exception ex ) {
            System.err.println( "Exception:: " + ex.getMessage() );
            ex.printStackTrace();
        }

        return new String[0];
    }

    /**
     * getMacAddresses - return all mac addresses found
     *
     * @param sep - use a different separator
     */
    public static void setSep( String sep )
    {
        try {
            MacAddressApplet.sep = sep;
        } catch( Exception ex ) {
            //  don't care
        }
    }

    /**
     * getMacAddresses - return all mac addresses found
     *
     * @param format - the output format string for bytes that can be overridden default hex.
     */
    public static void setFormat( String format )
    {
        try {
            MacAddressApplet.format = format;
        } catch( Exception ex ) {
            //  don't care
        }
    }

    public static void main( String... args )
    {
        System.err.println( " MacAddress = " + getMacAddress() );

        setSep( "-" );
        String macs [] = getMacAddresses();

        for( String mac : macs )
            System.err.println( " MacAddresses = " + mac );

        setSep( ":" );
        System.err.println( " MacAddresses JSON = " + getMacAddressesJSON() );
    }
}
Usage:

Code: Select all

<!--
 *
 * Author: Tim Desjardins
 * Copyright (c) 2008 Agwego Enterprises Inc.
 *
 * Feel free to use or abuse this code anyway you wish, without warranty of course.
-->
<html>
<head></head>
<body>

    <a href="" onclick="macs.getMacAddress();"> Get "first" MAC Address</a>
    <br/>
    <br/>
    <a href="" onclick="macs.getMacAddressesJSON();"> Get all MAC Addresses</a>

    <!--[if !IE]> Firefox and others will use outer object -->
    <embed type="application/x-java-applet"
           name="macaddressapplet"
           width="0"
           height="0"
           code="MacAddressApplet"
           archive="macaddressapplet.jar"
           pluginspage="http://java.sun.com/javase/downloads/index.jsp"
           style="position:absolute; top:-1000px; left:-1000px;">
        <noembed>
        <!--<![endif]-->
            <!---->
            <object classid="clsid:CAFEEFAC-0016-0000-FFFF-ABCDEFFEDCBA"
                    type="application/x-java-applet"
                    name="macaddressapplet"
                    style="position:absolute; top:-1000px; left:-1000px;"
                    >
                <param name="code" value="MacAddressApplet">
                <param name="archive" value="macaddressapplet.jar" >
                <param name="mayscript" value="true">
                <param name="scriptable" value="true">
                <param name="width" value="0">
                <param name="height" value="0">
              </object>
        <!--[if !IE]> Firefox and others will use outer object -->
        </noembed>
    </embed>
    <!--<![endif]-->

    <script type="text/javascript">
        var macs = {
            getMacAddress : function()
            {
                document.macaddressapplet.setSep( "-" );
                alert( "Mac Address = " + document.macaddressapplet.getMacAddress() );
            },

            getMacAddressesJSON : function()
            {
                document.macaddressapplet.setSep( ":" );
                document.macaddressapplet.setFormat( "%02x" );
                var macs = eval( String( document.macaddressapplet.getMacAddressesJSON() ) );
                var mac_string = "";
                for( var idx = 0; idx < macs.length; idx ++ )
                    mac_string += "\t" + macs[ idx ] + "\n ";

                alert( "Mac Addresses = \n" + mac_string );
            }
        }
    </script>

</body>

</html>
Post Reply

Return to “Java Programming”