Java Program To Display Hostname and IP Address
Java Program To Display Hostname and IP Address
Example
Live Demo
import java.net.*;
try{
}
}
}
Output
The IP address is : 127.0.0.1
A class named Demo contains the main function. In this main function, a ‘try’ and ‘catch’ block is
defined. In the ‘try’ block, an instance of InetAddress is created and the ‘getLocalHost’ function
is used to get the Host address and host name of the InetAddress instance. In case one of the
attributes is not found, the ‘catch’ block defines catching the exception and printing the relevant
message on the console.