1

How can I found the MAC address of a network card without knowing and using the IP address in Java?

user207421
  • 305,947
  • 44
  • 307
  • 483
Vlad V
  • 31
  • 1
  • 3

2 Answers2

1

you can use NetworkInterface.getHardwareAddress. You can get the list of all network cards with NetworkInterface.getNetworkInterfaces().

you can use this code for knowing MAC Address.

InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • I've tried like that but it doesn't work because it takes first the address of Local Loopback in linux. And to be more specific I need just the internet address of the network card which is connected on internet on linux, because on windows works the method gived by you. – Vlad V Apr 12 '17 at 15:00
0

You can use system command like below(if you on nix systems, if your os is windows use ipconfig in a similar way):

Runtime r = Runtime.getRuntime();
Process p = r.exec("ifconfig -u |grep ether");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";

while ((line = b.readLine()) != null) {
  System.out.println(line);
}

b.close();

On my system it gives:

ether 28:37:37:15:3b:31 
ether b2:00:10:65:56:41 
...

Notice that there are maybe many MAC addresses, the Bluetooth, the Ethernet, the WIFI and so on.

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57