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.

1
  • Are you trying to find in web application or desktop application ? Commented Dec 27, 2010 at 0:49

6 Answers 6

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();
     }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why an applet example? Quickly copypasted from Google?
@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 Applet is completely extraneous. What happens if you remove the extends Applet?
@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.
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.

Comments

0

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

1 Comment

YES! A link to Java 1.4 which is EOLifed ;)
0

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

Comments

0

Consider using the NetworkInterface class.

Comments

0

If you want to find ip address in java application

 InetAddress localIP=InetAddress.getLocalHost();

and if you are using web application

  request.getRemoteAddr();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.