---
title: "InetAddressTest 打印出指定网址的IP地址"
author: "rafale35"
date: "2010-07-06T15:48:20Z"
tags: []
categories: ["编程语言"]
slug: "inetaddresstest-ip"
draft: false
lastmod: "2010-07-06T15:48:20Z"
id: 706689
url: "https://rafale35.iteye.com/blog/706689"
---

```
package com.net.InetAddressTest;

import java.net.*;

/**
 * This program demonstrates the InetAddress class. Supply a host name as command line argument, or
 * run without command line arguments to see the address of the local host.
 * @version 1.01 2001-06-26
 * @author Cay Horstmann
 */
public class InetAddressTest
{
   public static void main(String[] args)
   {
      try
      {
         if (args.length > 0)
         {
            String host = args[0];
            InetAddress[] addresses = InetAddress.getAllByName(host);
            for (InetAddress a : addresses)
               System.out.println(a);
         }
         else
         {
            InetAddress localHostAddress = InetAddress.getLocalHost();
            System.out.println(localHostAddress);
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}
```