How to Find IP address of localhost or a Server in Java? Example (original) (raw)
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.
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:
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.