Java编程实现获取本机IP和计算机名的功能

时间:2022-06-20 08:07:21

版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.csdn.net/wangshuxuncom/article/details/35990847

import java.net.InetAddress;
import java.net.UnknownHostException; public class Test { public static void main(String[] args) {
try {
InetAddress inetAddress = InetAddress.getLocalHost();
System.out.println("本机IP:" + inetAddress.getHostAddress().toString());
System.out.println("本机名称:" + inetAddress.getHostName().toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}