System Ip Address In Java

System Ip Address In Java

Published on July 21 2021

In this tutorial, you will learn how to get the IP address and hostname of a local System in Java. We will also learn how to find IP address for google website in this tutorial.

Source code

package theprogrammingportal.javaexamplessourcecode;

/**
 *
 * @author TheProgrammingPortal
 */
import java.net.InetAddress;

public class IPAddress {

    public static void main(String[] args) {

        try {

            // get IP address and hostname for local system
            InetAddress myIP = InetAddress.getLocalHost();
            System.out.println(myIP);
            System.out.println(myIP.getHostName());
            System.out.println(myIP.getHostAddress());

            System.out.println("=======================");

            // get IP address and hostname for website
            InetAddress ip = InetAddress.getByName("www.google.com");
            System.out.println(ip);
            System.out.println(ip.getHostName());
            System.out.println(ip.getHostAddress());

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }
}

Video

 



Next Tutorial

  • System Ip Address In Java