Getting IP Address of Current Machine Using Java Program

Sharing is healthy !!

Hey guys!! Welcome to flower brackets blog. Today you will learn getting IP address of current machine using java program.

Getting IP Address of Current Machine Using Java Program

Getting IP Address of Current Machine Using Java Program

We all know our machine(Desktop or Laptop) Internet Protocol(IP) address just by opening Control Panel\Network and Internet\Network Connections, then selecting active network connection.

In that we have to click on details button to know Internet Protocol(IP) address.

The above said method is rather a simple way to know Internet Protocol(IP) address.

Today we will learn to write a java program that displays current machine Internet Protocol(IP) address.

Let’s get into example,

EXAMPLE: GETTING IP ADDRESS OF CURRENT MACHINE USING JAVA PROGRAM

Also Read – Create File Java Program

import java.net.InetAddress;

public class DisplayIPAddress
{
   public static void main(String[] args) throws Exception
   {
      System.out.println(InetAddress.getLocalHost());
   }
}

finding public ip address

Now let’s see how to get ip address in java. Here’s the code,

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

public class DisplayIPAddress
{
   public static void main(String[] args) throws UnknownHostException
   {
      InetAddress inet = InetAddress.getLocalHost();
      System.out.println("Machine IP Address : " + (inet.getHostAddress()).trim());
 
      String ipAddress = ""; // finding public IP address
      try
      {
         URL url = new URL("http:example.com");
 
         BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
 
         // reads system IPAddress
         ipAddress = br.readLine().trim();
      }
      catch (Exception ee)
      {
         ipAddress = "Oops!! Error occurred!!";
      }
      System.out.println("The public IP Address : " + ipAddress);
   }
}

getting the IP address and Hostname

Let’s see how to get ip address and hostname in java with an example,

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

public class DisplayHostName
{
   public static void main(String[] args)
   {
      try
      { 
         InetAddress inet = InetAddress.getLocalHost();
         // getHostAddress() - this method returns the IP address
         System.out.println("Machine IP address : " + inet.getHostAddress());
         // getHostName() - this method returns the host name
         System.out.println("Host name : " + inet.getHostName()); 
      } 
      catch(UnknownHostException uhe)
      { 
         System.out.println("Sorry!! Could not find local address and hostname!!"); 
      } 
   }
}

Output:

Machine IP address : 192.168.50.140
Host name : Flower Brackets


conclusion

That’s it guys. This was all about how to get IP address in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Sharing is healthy !!