4

I am trying to create a Java program to retrieve my systems IP address using Java. On the click of a button, the IP address is displayed is a text box.

IP in java using Netbeans

Now the problem is, everytime I use the code

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        jTextField1.setText(Inet4Address.getLocalHost().toString());
    } catch (UnknownHostException ex) {
        jTextField1.setText("IP not found");
    }
}  

It only retrieves my loopback IP and not the static IP which I have configured in Ubuntu which is of the range 192.16x.xxx.xxx This piece of code works fine on my windows 7 but not on Ubuntu 11.04, I dont know why...!!! I dont wanna use windows for developing applications or anything.

Just 2 questions I have

  1. How do I retrieve my systems static IP ?
  2. If java is cross-platform, why does this piece of code works well on windows & not on Ubuntu ?
MByD
  • 135,866
  • 28
  • 264
  • 277
Roshan
  • 2,604
  • 2
  • 15
  • 13
  • The picture is in the link https://picasaweb.google.com/110032752637088824628/SystemIPStackoverflow?authkey=Gv1sRgCPTNvLD3qbqRJw#5638314483328453106 – Roshan Aug 08 '11 at 03:13
  • See also http://stackoverflow.com/questions/9481865/how-to-get-ip-address-of-our-own-system-using-java – Vadzim Dec 06 '13 at 06:39

1 Answers1

7

Your problem looks very similar to this one in stackoverflow, maybe the answer will help you ;)

Community
  • 1
  • 1
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132