This question already has an answer here:
Im trying to get the local IP. It should work with
System.out.println(Inet4Address.getLocalHost().getHostAddress());
or
InetAddress addr = InetAddress.getLocalHost();
ip = addr.getHostAddress();
System.out.println("Ip: " + ip);
but it always returns 192.168.178.154
instead of 192.168.178.119
(This is my real local IP(Terminal --> ifconfig
))
What should I do?
if your system is configured with multiple ip then do like this.
Sounds like you have two IP addresses.
Document
If you want to activate second IP and its for example LAN, unplug it and after 10 sec plug in back. Other IP might be selected as host IP in routing table.
You can get 2nd IP from
getNetworkInterfaces
.Try to run followed code: