Monday, March 09, 2009

Detect IP Address and Name in Java

java.net.InetAddress Class has a static method getLocalHost method which returns Object of localHost Then GetHostAddress Method Returns the IP address string in textual presentation. So in Your program use following line to print IP Address.

import java.net.*;
public class IPAdress {
public static void main(String[] args) {

try {
System.out.println(InetAddress.getLocalHost().getHostName());
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

source: java-tips.org

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory