Java中获取Linux的IP地址可以使用InetAddress类的相关方法。下面是获取本地IP地址的示例代码:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) {
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("IP Address: " + localhost.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
这段代码使用getLocalHost()
方法获取本地主机的InetAddress对象,然后使用getHostAddress()
方法获取IP地址。