简体   繁体   中英

How can i check System IP Address/Host Name in Java?

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.

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 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.

If you want to find ip address in java application

 InetAddress localIP=InetAddress.getLocalHost();

and if you are using web application

  request.getRemoteAddr();

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

考虑使用NetworkInterface类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM