Q:

write and execute a java program to find the hostname and MAC address of a computer

0

write and execute a java program to find the hostname and MAC address of a computer

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

package com.mycompany.mavenproject2;

import java.net.*;
import java.net.InetAddress;

public class NewClass {
     public static void main(String args[]) throws Exception
    {
      InetAddress inetAddress = InetAddress.getLocalHost();
      String hostName = inetAddress.getHostName(); //Get Host Name
      String macAddress = "";
      //Get MAC Address
      NetworkInterface network = NetworkInterface.getByInetAddress(inetAddress);
      byte[] macArray = network.getHardwareAddress();
      StringBuilder str = new StringBuilder();
      // Convert the macArray to String
      for (int i = 0; i < macArray.length; i++) {
        str.append(String.format("%02X%s", macArray[i], (i < macArray.length - 1) ? " ": ""));
        macAddress = str.toString();
      }
      System.out.println("Host Name: " + hostName);
      System.out.println("MAC Address: " + macAddress);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now