The class instantiates an InetAddress object through the IP address provided as
commandline argument and invokes the getHostName() through that object which
returns the Hostname for the corresponding IP.
Incase of any invalid IP an UnknownHostException is thrown which is handeled in code by displaying an error message.
The point to be noted is that while providing IP address through command line leave a single blank space after the class name.
Incase of any invalid IP an UnknownHostException is thrown which is handeled in code by displaying an error message.
The point to be noted is that while providing IP address through command line leave a single blank space after the class name.
import java.net.InetAddress; import java.net.UnknownHostException; public class Hostname { public static void main ( String[] args ) { String IP = args[0]; try { InetAddress[] addresses = InetAddress.getAllByName(IP); for ( int i=0; i<addresses.length; i++ ) { String hostname = addresses[i].getHostName(); //String hostname = addresses[i].getHostAddress(); System.out.println( hostname ); } } catch ( UnknownHostException e ) { System.out.println( "Could not resolve IP Address to Hostname.....Unknown Host" ); } } }
thanks