1

is there any way to find out system IP Address without using external process? I want to grab this information for my application but in pure java if possible.

SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
Space Rocker
  • 787
  • 3
  • 11
  • 25

6 Answers6

3

Does this meet your needs?

import java.net.*;
import java.io.*;
import java.applet.*;

public class GetClientIP extends Applet {
  public void init() {
    try {
     InetAddress thisIp =
        InetAddress.getLocalHost();
     System.out.println("IP:"+thisIp.getHostAddress());
     }
    catch(Exception e) {
     e.printStackTrace();
     }
    }
}
THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64
  • 1
    Why an applet example? Quickly copypasted from Google? – BalusC Dec 27 '10 at 01:01
  • @BalusC - Why not an applet? What difference does it make if it was copy/paste or not? This is not an unusual request that would require custom code to be written at all. The example demonstrates the concept whether OP desires to make an applet, a class, or anything else. – THE DOCTOR Dec 27 '10 at 01:07
  • The Applet is completely extraneous. What happens if you remove the `extends Applet`? – President James K. Polk Dec 27 '10 at 02:10
  • @GregS - Yes, one could remove import java.applet.*; and extends Applet. It would work just as well. It makes no difference and does not affect functionality. – THE DOCTOR Dec 27 '10 at 02:44
1

The InetAddress.getLocalHost().getHostAddress() call doesn't always work; sometimes it returns 127.0.0.1.

See java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP? for some more details and other options.

Community
  • 1
  • 1
Eric
  • 5,137
  • 4
  • 34
  • 31
0

If you want to find ip address in java application

 InetAddress localIP=InetAddress.getLocalHost();

and if you are using web application

  request.getRemoteAddr();
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Ratna Dinakar
  • 1,573
  • 13
  • 16
0

Have a look at InetAddress http://download.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
0

Hi yes its possible with the class InetAddres. Check this link JAVA API and the method you need is getHostAddress()

Jorge Machado
  • 752
  • 1
  • 8
  • 28
0

Consider using the NetworkInterface class.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125