在Java中,可以使用InetAddress
类来获取本机的IP地址。以下是一个示例代码:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetIPAddress {
public static void main(String[] args) {
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("IP Address: " + localhost.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
运行上述代码将打印出本机的IP地址。