1. 程式人生 > >java解析主機名獲取ip地址

java解析主機名獲取ip地址

import java.net.*;
/**
 * 解析主機名獲取ip地址
 */
public class GetIpByHostName {
    public static void main(String[] args){
        InetAddress address = null;
        if(args.length == 0){
            System.out.println("usage: getip host");
            System.exit(1);
        }
        try{
            address = InetAddress.getByName(args[0]);
        }catch(Exception e){
            System.out.println("not found");
            System.exit(2);
        }
        System.out.println(address.getHostName() + "=" + address.getHostAddress());
        System.exit(0);
    }
}