

Hi,
Here is the answer.
We get the hostname of the local ip address. This example is helpful for you.
import java.net.InetAddress;
import java.net.UnknownHostException;
public class staticdemo {
public static void main(String[] args) {
try {
InetAddress localaddr = InetAddress.getLocalHost();
System.out.println("main Local IP Address : "
+ localaddr.getHostAddress());
System.out.println("main Local hostname : "
+ localaddr.getHostName());
System.out.println();
InetAddress[] localaddrs = InetAddress.getAllByName("localhost");
for (int i = 0; i < localaddrs.length; i++) {
if (!localaddrs[i].equals(localaddr)) {
System.out.println("alt Local IP Address : "
+ localaddrs[i].getHostAddress());
System.out.println("alt Local hostname : "
+ localaddrs[i].getHostName());
System.out.println();
}
}
} catch (UnknownHostException e) {
System.err.println("Can't detect localhost : " + e);
}
}
}
Thanks.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.