import java.net.*;
public class test {
public static InetAddress ina;
public static void main(String [] args){
try{
ina = InetAddress.getByName("sharon");
System.out.println(ina.getHostAddress());
}
catch(Exception e){
System.out.println(e);
}
}
}
其中sharon是我的机器名,换成你的机器名就可以了。

解决方案 »

  1.   


    import java.net.InetAddress;public class getIp { static  void getIp() {
        try{
           InetAddress Ipaddress = InetAddress.getLocalHost();
            System.out.println(Ipaddress.toString());
        }
        catch(Exception e){
            System.out.println(e);
        }
      }   public static void main(String [] args){
          getIp();  }}
      

  2.   

    Thinking In Java 上有这个例子啊!
      

  3.   

    跪求大家,帮我看看这个问题,谢谢!!!!!!!!!!!!!http://expert.csdn.net/Expert/topic/2633/2633574.xml?temp=.6377375
      

  4.   

    try {
            // Get hostname by textual representation of IP address
            InetAddress addr = InetAddress.getByName("127.0.0.1");
        
            // Get hostname by a byte array containing the IP address
            byte[] ipAddr = new byte[]{127, 0, 0, 1};
            addr = InetAddress.getByAddress(ipAddr);
        
            // Get the host name
            String hostname = addr.getHostName();
        
            // Get canonical host name
            String hostnameCanonical = addr.getCanonicalHostName();
        } catch (UnknownHostException e) {
        }
    --------------------------------------------------------------
        try {
            InetAddress addr = InetAddress.getByName("javaalmanac.com");
            byte[] ipAddr = addr.getAddress();
        
            // Convert to dot representation
            String ipAddrStr = "";
            for (int i=0; i<ipAddr.length; i++) {
                if (i > 0) {
                    ipAddrStr += ".";
                }
                ipAddrStr += ipAddr[i]&0xFF;
            }
        } catch (UnknownHostException e) {
        }