How to get System IP using Java?
I was trying to make a jFrame which had a button & a text-area/label, the motive being able to retrieve my systems IP Address, the problem is, when I use this code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
InetAddress ownIP=InetAddress.getLocalHost();
jTextField1.setText(ownIP.getHostAddress());
开发者_如何学JAVA }
catch (Exception e)
{
jTextField1.setText(e.getMessage());
}
}
But then this gives me back the loop back IP Address, 127.0.0.1 :( I have static IP configured on my system, but then too that IP does not show up I use NetBeans IDE 7.0 & Ubuntu 11.04
You can obtain all IP addresses for your system. Use the NetworkInterface.getNetworkInterfaces() method to retrieve all of the network interfaces. For each of the returned interfaces, use the getInetAddresses() method to retrieve all of the associated addresses.
FWIW, InetAddress.getLocalHost().getHostAddress()
gives me my real ip address.
FYI, I ran it from a unit test within Eclipse on a macbook.
精彩评论