Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get InetAddress.getLocalHost.getHostAddress (Java/Scala) to return external IP

So I'm having a problem with using InetAddress.getLocalHost.getHostAddress to get the external IP address of a given machine.

I'm actually doing this in Scala in a sense - the configuration file for Akka Remote Actors default uses InetAddress.getLocalHost.getHostAddress to get the IP address of the machine, which is what I want since I will be deploying the actors on several machines. However, it seems to be returning 127.0.0.1 instead of the external IP address I want (since the remote actors need to communicate back and forth across the netwrok).

The problem is that I can't use any of the methods I've found on Google to circumvent this since they all seem to involve adjusting the code itself, whereas here I don't really have any code to adjust, the DSL just automatically uses InetAddress.getLocalHost.getHostAddress.

I've read on a few threads from a Google search that you can circumvent this by editing your host file or something? How do I do this?

Thanks! -kstruct

like image 333
adelbertc Avatar asked Sep 17 '25 17:09

adelbertc


1 Answers

You may want to use NetworkInterface class.

In particular, use static getNetworkInterfaces method to enumerate all available network interfaces.

like image 156
Alexander Pogrebnyak Avatar answered Sep 20 '25 05:09

Alexander Pogrebnyak