java解析主机名获取ip地址

来源:互联网 发布:软件开发体系结构 编辑:程序博客网 时间:2024/05/20 21:43
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);    }}
原创粉丝点击