Get Local IP Address and Hostname In Java

 Posted On  | Yashwant Chavan 

Below Example determine Local IP Address and Hostname of local computer using Java API.

java.net.InetAddress - class represents an Internet Protocol (IP) address.

java.net.InetAddress.getLocalHost() - Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.

java.net.InetAddress.getHostAddress() - Returns the IP address string in textual presentation.

java.net.InetAddress.getHostName() - Gets the host name for this IP address.

package com.technicalkeeda.app;

import java.net.InetAddress;

class IPAddressExample {
    public static void main(String args[]) throws Exception {
        InetAddress inetAddress = InetAddress.getLocalHost();
        System.out.println("IP Address:- " + inetAddress.getHostAddress());
        System.out.println("Host Name:- " + inetAddress.getHostName());
    }
}

output

IP Address:- 169.254.151.193
Host Name:- swami-samartha


Promote your Blog, Website, or services on technicalkeeda.com, My paypal id: yashwantchavan@gmail.com

Payment will be accepted through paypal only.


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0302 seconds.