Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

get the mac addresses of the localhost

843790Sep 9 2010 — edited Sep 9 2010
hi,
i am trying to get all the mac address of the localhost network interfaces.
here is my code. i don't seem to find what is wrong with it.

i will appreciate any help.
thanks
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MAC {
	public static void main(String[] args) {
		try { 
			InetAddress[] addrs = null;
			if((addrs = InetAddress.getAllByName("localhost")) == null) System.exit(1);
			
			NetworkInterface ni = null;
			for(int i = 0; i < addrs.length; ++i) {
				if((ni = NetworkInterface.getByInetAddress(addrs)) == null) 	System.exit(2);

byte[] mac = null;
if((mac = ni.getHardwareAddress()) == null) System.exit(3);

System.out.println(addrs[i]);
System.out.println(new String(mac));
}
}
catch(UnknownHostException e) { System.out.println(e.getMessage()); }
catch(SocketException e) { System.out.println(e.getMessage()); }
}
}
Edited by: BigJ___ on Sep 9, 2010 2:44 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 7 2010
Added on Sep 9 2010
9 comments
1,135 views