在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地址。