InetAddress Example program in Java

By: Grenfel Emailed: 1730 times Printed: 2344 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The InetAddress class has no visible constructors. To create an InetAddress object, you have to use one of the available factory methods. Factory methods are merely a convention whereby static methods in a class return an instance of that class. This is done in lieu of overloading a constructor with various parameter lists when having unique method names makes the results much clearer. In the case of InetAddress, the three methods getLocalHost(), getByName(), and getAllByName() can be used to create instances of InetAddress. These methods are shown here:

static InetAddress getLocalHost( )
throws UnknownHostException
static InetAddress getByName(String hostName)
throws UnknownHostException
static InetAddress[ ] getAllByName(String hostName)
throws UnknownHostException

The getLocalHost( ) method simply returns the InetAddress object that represents the local host. The getByName( ) method returns an InetAddress for a host name passed to it. If these methods are unable to resolve the host name, they throw an UnknownHostException.

On the Internet, it is common for a single name to be used to represent several machines. In the world of web servers, this is one way to provide some degree of scaling. The getAllByName( ) factory method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. It will also throw an UnknownHostException if it can't resolve the name to at least one address.

The following example prints the addresses and names of the local machine and two well-known Internet web sites:

// Demonstrate InetAddress.
class InetAddressTest
public static void main(String args[]) throws
UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
Address = InetAddress.getByName("");
InetAddress SW[] = InetAddress.getAllByName("");
for (int i=0; i<SW.length; i++)

Here is the output produced by this program. (Of course, the output you see will be slightly different.)


This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

more info needs to be given ............

View Tutorial          By: sufficient for new commer at 2008-05-09 00:41:58
2. View Comment

excellent for a beginer in inet address ...

View Tutorial          By: Tony at 2009-08-13 22:39:48
3. View Comment

Can you give me an example to create InetAddress from IP address like

View Tutorial          By: Vaibhav at 2009-12-15 23:37:49
4. View Comment

Would be great if you also showed how to manipulate the output too.

View Tutorial          By: Sparx at 2010-01-05 13:51:56
5. View Comment

good but very simple program.........

View Tutorial          By: vipul at 2010-03-25 00:33:54
6. View Comment

it shows error
java.lang.NoClassDefFoundError: inetaddresstest/InetAddressTest
Caused by: java.lang.ClassNotFoundException: inetaddresstest.InetAddressTest
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
Could not find the main class: inetaddresstest.InetAddressTest. Program will exit.
Exception in thread "main"
Java Result: 1

View Tutorial          By: vamsi at 2010-05-14 23:09:03
7. View Comment

excellent notes

View Tutorial          By: jawahar at 2010-08-03 00:23:52
8. View Comment

I am getting

Exception in thread "main"
at Test.main(
what might be the reason?

View Tutorial          By: GSA at 2010-11-23 02:36:26
9. View Comment

@Vaibhavn if you use an IP instead of a host name it works too!

View Tutorial          By: RDMA at 2011-01-04 14:30:15
10. View Comment

Does anybody know how to force using IPv6 by my laptop??
I connect my computer to another one locally, disable IPv4 stack on my NIC, assign static IPv6 address (which is displayed and confirmed by ipconfig command in cmd) and after all what I get from these two lines of code?

InetAddress Address = InetAddress.getLocalHost();


What is wrong?what I need to change to work with my IPv6 address??please, give me some advice..

View Tutorial          By: Marcin at 2011-05-17 05:24:51
11. View Comment

HI all i would like to send the result of " InetAddress Address = InetAddress.getLocalHost(); " to a Jtextfield , so that I can display it in a gui like so:

| TITLE: My super gui that looks very nice! - [] X |
| |-------------------------------------------| |
| | My Ip address in a JTextField | <-JTxtField|
| |-------------------------------------------| |
| |
| |
\---------------------------------------- ----------------------/

I'm pretty new to java so please don't be to technical. ;-)

Thanks in advance

View Tutorial          By: FLORIAN at 2011-09-14 09:04:42
12. View Comment

how can get ip url...?

View Tutorial          By: arjuna at 2011-11-23 03:31:46
13. View Comment

How to display the ARP Mapping Table of a given host ?

View Tutorial          By: Hala at 2013-11-15 22:42:36

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Grenfel
Verifying a Signed JAR File example
Bitwise and Bit Shift Operators example in Java
JavaBeans Basic Concepts
The Differences Between Simple and Classic Tags in JSP
Bitwise Logical Operators in C
DispatchAction in Struts
union example program in C
Comparison operators in JSP
Basics of Structures in C
register Variables in C
Struts and Tiles - Steps to use Struts and Tiles
The if else statement in C
Character Counting sample program in C
Installing JSF
cin.ignore() in C++

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Student marks calculation program in Java
Count number of vowels, consonants and digits in a String in Java
XML and Java - Parsing XML using Java Tutorial
What is Java?
FileReader and FileWriter example program in Java
Stack example in Java - push(), pop(), empty(), search()
Method Overriding in Java
arraycopy example in Java
Read from a COM port using Java program
How to use ArrayList in Java
Method Overloading (function overloading) in Java
InetAddress Example program in Java
The Basic Structure of a Simple Java program
The switch Statement example in Java
Most Emailed Articles (in Java)
The Unsigned Right Shift
Reverse a number in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to display numbers from 1 to 10 on Applet
Insert, Delete, Search, Print an int Array in Java
Steps in using verisign certificate with Glassfish appserver
Step by Step guide to setup freetts for Java
Method Overloading sample in Java
Method Override sample in Java
How Java is Organized?
Setting Up SSL on Tomcat
Compare compareTo() function of String class with Collator and CollationKey
Applet Notinited : Applet xxxxxxxxxxxx notinited
The break Statement example in Java