In today's Java programming tutorial, we will learn some networking basics by exploring the java.net package. One of the simple Java network programming exercises, yet very useful, is to write a program to find the IP address of the local host in Java. Sometimes this question is also asked to find the IP address of the Server on which your Java program is running or find the IP address of your machine using Java etc. In short, they all refer to localhost. For those who are entirely new in the networking space, there are two things to identify a machine in a network, which could be LAN, WAN, or The Internet.
The first thing is the DNS name and the second thing is the IP address. In Local Area Network, DNS is more generally replaced by hostname, which could be as simple as ReportingServer to ReportingServer.com or something else.
Both these hostname and IP addresses are related to each other, which means given hostname, your computer can find an IP address and vice-versa. The file they use for a hostname to IP address resolution is /etc/host in both Windows 8 and Linux. Java provides API to get this IP address by providing hostname.
Since localhost is used to refer to the machine on which the program is running, you can provide that to get an IP address or your Server, or your desktop in Java. By the way, if you are doing support and need it for real purposes there are more easy windows and Linux commands to convert hostname to IP address and vice-versa, you can check them here.
Both these hostname and IP addresses are related to each other, which means given hostname, your computer can find an IP address and vice-versa. The file they use for a hostname to IP address resolution is /etc/host in both Windows 8 and Linux. Java provides API to get this IP address by providing hostname.
Since localhost is used to refer to the machine on which the program is running, you can provide that to get an IP address or your Server, or your desktop in Java. By the way, if you are doing support and need it for real purposes there are more easy windows and Linux commands to convert hostname to IP address and vice-versa, you can check them here.
Java program to find the IP address of Localhost
Here is our sample program to show the IP address of the localhost. The key class to know is InetAddress, this class encapsulates hostname and IP address information. It provides a static method, called InetAddress.getLocalHost() to get an instance of this class corresponding to localhost.This may throw java.net.UnknownHostException if name localhost is not specified in your /etc/host file, in general, if there is any issue while resolving that hostname.
Once you get an instance of InetAddress, you can call getHostAddress() method to retrieve the IP address of the host. If you defined another computer name for your host, apart from the default alias localhost, you can get that name by calling getHostName() method.
By the way, f you need to find the IP address of your server, you should use a Linux command like nslookup or hostname. If you are not familiar with essential Linux commands then you should check these free Linux courses to learn about IP addresses and other basic networking concepts which are quite useful for Java developers.
And, here is our complete Java program to retrieve hostname and IP address or any machine in Java:
That's all on how to find the IP address of your machine in Java. As I said before, since your machine is also referred to as localhost, you can use this program to answer the question, which asks about finding the IP address of localhost in Java. In the next article, we will learn about how to find the IP address of any host in the Java program. I am also thinking to share some basics of socket programming using traditional IO and NIO, let me know if you guys are interested in that.
import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; /** * Java program to find IP address and hostname of a machine. InetAddress of * java.net package is used to get IP address and hostname in Java applications. * * @author WINDOWS 8 * */ public class IPAddress { public static void main(String args[]) { // First get InetAddress for the machine, here localhost InetAddress myIP = null; try { myIP = InetAddress.getLocalHost(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } // getHostAddress() returns IP address of a machine String IPAddress = myIP.getHostAddress(); // getHostname returns DNS hostname of a machine String hostname = myIP.getHostName(); System.out.printf("IP address of Localhost is %s %n", IPAddress); System.out.printf("Host name of your machine is %s %n", hostname); } } Output: IP address of Localhost is 192.168.1.4 Host name of your machine is Javarevisited
That's all on how to find the IP address of your machine in Java. As I said before, since your machine is also referred to as localhost, you can use this program to answer the question, which asks about finding the IP address of localhost in Java. In the next article, we will learn about how to find the IP address of any host in the Java program. I am also thinking to share some basics of socket programming using traditional IO and NIO, let me know if you guys are interested in that.
Hay nice arcticle, but i like to point out that this method most times return the loopback address, which might not be too helpful. The code could be modified as stated below to get the address of all interfaces and need be can also be modified for specific interfaces.
ReplyDeleteimport java.net.*;
import java.util.*;
public class IPAddress {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress myIP = null;
try
{
myIP = InetAddress.getLocalHost();
} catch (UnknownHostException e)
{
e.printStackTrace();
}
String IPAddress = myIP.getHostAddress();
String hostname = myIP.getHostName();
System.out.printf("IP address of LocalHost is %s %n", IPAddress);
System.out.printf("Host name of my machine is %s %n", hostname);
// System.out.println("Host addr: " + InetAddress.getLocalHost().getHostAddress()); // often returns "127.0.0.1"
Enumeration n = null;
try {
n = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (; n.hasMoreElements();)
{
NetworkInterface e = n.nextElement();
System.out.println("Interface: " + e.getName());
Enumeration a = e.getInetAddresses();
for (; a.hasMoreElements();)
{
InetAddress addr = a.nextElement();
System.out.println(" " + addr.getHostAddress());
}
}
}
}
Thanks mate i was stuck with localhost thing .great help
ReplyDeleteWhen i wrote this code, i got an NullPointerException
ReplyDeleteimport java.net.InetAddress;
public class MySystemInfo {
public static void main(String[] args) {
try
{
InetAddress my=null;
System.out.println("My Name is :" +my.getHostName());
System.out.println("My IpAddress is :"+my.getHostAddress());
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
InetAddress myIP = null;
ReplyDeletetry{
myIP = InetAddress.getLocalHost();
}catch (UnknownHostException e){
e.printStackTrace();
}
assert myIP != null;
String ipAddress = myIP.getHostAddress();
System.out.printf("Ip address = %s %n",ipAddress);
This code return 127.0.0.1 instead of actual ip...
That's because localhost is mapped to 127.0.01, do you want your public IP address?
DeleteJavin Paul, I want to public IP of my server machine. Can you please guide me?
DeleteOpen a website like IPchicken.com from your host. If you can.
Delete