If your Java application is running on a computer on the Internet, it already has DNS, and it already has at least one IP address visible to other computers on your local network. Use Java code similar to what I wrote below to get the IP address.
import java.net.*; import java.io.*; public class Ip { public static void main ( String[] args ) throws IOException { String hostname = args[0]; try { InetAddress ipaddress = InetAddress.getByName(hostname); System.out.println("IP address: " + ipaddress.getHostAddress()); } catch ( UnknownHostException e ) { System.out.println("Could not find IP address for: " + hostname); } } }
PS. if the IP address of the machine on which you run the Java server application changes (it works on the home computer, and the ISP assigns a dynamic IP address), then use a free service, for example http://www.dyndns.com or the like . In this case, it becomes a bit complicated because you have to report a change in the dynamic IP DNS. Some routers have a built-in feature, some do not. In this case, you must ensure that dynamicDNS is informed. There are many scripts on the Internet that do this for you (usually Linux / UNIX), and there are also some free Windows tools. I never did this on Windows, but I did it on Linux, and it works well.
source share