Java Program to Get IP Address

This article covers multiple programs in Java that find and prints IP address. Here are the list of programs covered by this article:

Find and Print Local IP Address in Java

The question is, write a Java program to print the local IP address of a computer system. The program given below is its answer:

import java.net.InetAddress;

public class fresherearth
{
   public static void main(String[] args)
   {
      try
      {
         InetAddress ip = InetAddress.getLocalHost();
         System.out.println("IP Address = " +ip.getHostAddress());
      }
      catch(Exception e)
      {
         System.out.println("Exception: " +e);
      }
   }
}

The snapshot given below shows the sample output produced by above Java program:

java find print ip address

Code executed on local system, will print local IP. But no worry, other options are also available to find the public IP. Let's see how, in the section given below.

Find and Print Public IP Address in Java

To check your public IP address, you need to visit any website that shows your system's public IP address. For example, amazonaws shows the IP address of your computer system. And of course, that will be your public IP address. To see, open your browser, and type checkip.amazonaws.com. You'll get your public IPv4 address.

There are a lot of websites, from where you can find your public IP, but since Amazon is trusted one, therefore I've used their URL.

Now, I'm going to use this web address in my Java program to get the value generated by this URL for my system. And that value will get printed on the output, to show the public IPv4 address:

import java.net.*;
import java.io.*;

public class fresherearth
{
   public static void main(String[] args)
   {
      try
      {
         URL ip = new URL("https://checkip.amazonaws.com");
         BufferedReader br = new BufferedReader(new InputStreamReader(ip.openStream()));
         String pip = br.readLine();
         System.out.println("Public/External IP Address = " +pip);
      }
      catch(Exception e)
      {
         System.out.println("Exception: " +e);
      }
   }
}

Here is its sample output:

java find print public external ip address

In your case, the output will be different.

Find Local and Public IP Address using Java

This is the combined version of previous program. This program prints both local and public IP address of a computer system.

import java.net.*;
import java.io.*;

public class fresherearth
{
   public static void main(String[] args)
   {
      try
      {
         InetAddress localhost = InetAddress.getLocalHost();
         System.out.println("Local IP Address = " +localhost.getHostAddress());
      }
      catch(Exception e)
      {
         System.out.println("Exception: " +e);
      }
      try
      {
         URL ipfinder = new URL("https://checkip.amazonaws.com");
         BufferedReader br = new BufferedReader(new InputStreamReader(ipfinder.openStream()));
         String publicIP = br.readLine();
         System.out.println("Public IP Address = " +publicIP);
      }
      catch(Exception e)
      {
         System.out.println("Exception: " +e);
      }
   }
}

The sample output produced by above program is shown in the snapshot given below:

java find local public ip address

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »