Skip to content

Instantly share code, notes, and snippets.

@zubayerahamed
Last active April 28, 2018 03:29
Show Gist options
  • Save zubayerahamed/0726b713463a194db8ea14f6459c8c0c to your computer and use it in GitHub Desktop.
Save zubayerahamed/0726b713463a194db8ea14f6459c8c0c to your computer and use it in GitHub Desktop.
Find PC Mac Address and IP address using java
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class App {
public static void main(String[] args) {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
} catch (UnknownHostException | SocketException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment