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.
6 Answers
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();
}
}
}
4 Comments
BalusC
Why an applet example? Quickly copypasted from Google?
THE DOCTOR
@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.
President James K. Polk
The Applet is completely extraneous. What happens if you remove the
extends Applet?THE DOCTOR
@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 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
Have a look at InetAddress http://download.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html
1 Comment
SyntaxT3rr0r
YES! A link to Java 1.4 which is EOLifed ;)
Hi yes its possible with the class InetAddres. Check this link JAVA API and the method you need is getHostAddress()