Roshan
Roshan

Reputation: 2762

How to get System IP using Java?

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 ?

Upvotes: 4

Views: 2819

Answers (1)

Cédric Julien
Cédric Julien

Reputation: 80761

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

Upvotes: 7

Related Questions