Java program to display Local machines IP Address and Hostname

Write a Java program to display Local machines IP Address and Hostname

in following example program we are getting system’s IP by getting the Host address of the local host.

InetAddress ownIP=InetAddress.getLocalHost(); gets the computer system’s local host with IP address.

Now we can get the IP address with this object by calling methodgetHostAddress() on the InetAddress object.

Here is the full example code of GetOwnIP.java as follows:


[java]
import java.util.*;
import java.lang.*;
import java.net.*;

public class Ip_Add
{
public static void main(String args[]) {
try{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ownIP.getHostAddress());
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
}
}

[/java]


Display Local machines IP Address and Hostname

[java]
import java.net.*;
import java.io.*;

public class ip
{
public static void main(String args[]) throws Exception
{
InetAddress ipadd =InetAddress.getLocalHost();
System.out.println("Host and Address :"+ipadd);
System.out.println("Host name :"+ipadd.getHostName());

String n=ipadd.toString();
System.out.println("IP address :"+n.substring(n.indexOf("/")+1));
}
}

[/java]

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.