Convert Hostname to IP Address using Java

InetAddress class

package app;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main
{
    public static void main(String[] args) throws UnknownHostException
    {
        String hostname = "google.com";
        InetAddress address = InetAddress.getByName(hostname);
        String ip = address.getHostAddress();

        System.out.println(ip);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.