可以使用getLocalHost
命令:
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());
System.out.println("Local host name:" + addr.getHostName());
}
}
即可输出本机的IP和name。
学习更多编程知识,请关注我的公众号: