In today's Java programming tutorial, we will learn some networking basics by exploring
java.net package. One of the simple Java network programming exercise, yet very useful, is to
write a program to find IP address of local host in Java. Some time this question also asked as find IP address of Server on which your Java program is running, or
find IP address of your machine using Java etc. In short, they all refer to localhost. For those who are entirely new in networking space, there are two things to identify a machine in a network, which could be LAN, WAN or Internet. First thing is
DNS name and second thing is
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 this
hostname and IP address are related to each other, which means given hostname, your computer can find IP address and vice-versa. The file they use for 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 the machine on which program is running, you can provide that to get IP address or your Server, or your desktop in Java. By the way, if you are doing support and need it for real purpose there are more easy windows and Linux commands to convert hostname to IP address and vice-versa, you can check them
here.