Skip to content

Instantly share code, notes, and snippets.

@roshanadh
Last active April 10, 2019 06:58
Show Gist options
  • Save roshanadh/0f402b3ef2a941fe61063562c54bb41a to your computer and use it in GitHub Desktop.
Save roshanadh/0f402b3ef2a941fe61063562c54bb41a to your computer and use it in GitHub Desktop.
Get IP address from a given Hostname in Java
import java.net.*;
import java.io.*;
public class HostNameDemo
{
public static void main(String[] args) throws IOException
{
System.out.println("Type the Host Name: ");
BufferedReader readName=new BufferedReader(new InputStreamReader(System.in));
String hostName=readName.readLine();
try{
InetAddress host= InetAddress.getByName(hostName);
System.out.println("The address for "+hostName+" is: "+host.getHostAddress());
}
catch(UnknownHostException e)
{
System.out.println("IP address was not found for given Host name.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment