Home Java Java-get-example Java example program to get IP address of own system
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java example program to get IP address of own system

Advertisement
An IP (Internet Protocol) address is a numerical unique identification number which is assigned to the devices participating in the network for communication.

Java example program to get IP address of own system

     

java get own IP address

An IP (Internet Protocol) address is a numerical unique identification number which is assigned to the devices participating in the network for communication. To get IP address of our own system in a java program we will be using InetAddress class.

In our 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 method getHostAddress() on the InetAddress object.

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

GetOwnIP.java

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

public class GetOwnIP
{
  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());
  }
  }
}

Output:

C:\javaexamples>javac GetOwnIP.java

C:\javaexamples>java GetOwnIP
IP of my system is := 192.168.10.112

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Related Tutorials

Ask Questions?    Discuss: Java example program to get IP address of own system   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Abhi
August 12, 2011
thx

dats wat i wanted... it works.. wat If i want to know IPs of aall systems connected to my network??
srsreddy
August 23, 2011
000

yes,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Ryan Grapp
February 23, 2012
Thanks

All this does is display 127.0.0.1. I want the IP address assigned to me in the network. So.. like if I'm behind a router and I've been assigned. 192.168.1.1
deepika
March 5, 2012
java code to find system id and port number

Any one please help me for projects please
xooxa
April 18, 2012
Thanks for IP check

Thank you for this..... I appreciate people who give examples like this and its a great help....
Erina Wilson
July 8, 2014
about java

This site was really usefull
DMCA.com