以下实例演示了如何使用 InetAddress 类的 getLocalAddress() 方法获取本机ip地址及主机名:
/* author by w3cschool.cc Main.java */import java.net.InetAddress;public class Main { public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress: "+addr.getHostAddress()); String hostname = addr.getHostName(); System.out.println("Local host name: "+hostname); }
以上代码运行输出结果为: 以上就是Java 实例 - 获取本机ip地址及主机名的内容,更多相关内容请关注PHP中文网(www.php.cn)!Local HostAddress: 192.168.1.4Local host name: harsh