In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app.
package com.mkyong;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
} catch (UnknownHostException e) {
e.e.printStackTrace();
}
}
}
Output
Current IP address : 192.168.1.22
it returns 127.0.0.1
Hi ,
How to get remote server (client server) IP address from soap request in java.
Great, it works
Can I get others server IP in LAN. like samba server ?
This example is always giving the IP Address from the /etc/hosts (linux), if we change the machine IP from static to dhcp or dhcp to static it will not give the correct result.
How i can get public Ip address of my network in which i connected.
How would I save the IP address to a string or write it to a file?
import java.net.InetAddress;
import java.net.UnknownHostException;
public class getIpAddress {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
InetAddress ownIP = InetAddress.getLocalHost();
//System.out.println(“My IP Address : “+ ownIP.getHostAddress());
String myIp = ownIP.getHostAddress();
System.out.println(“IP :”+myIp);
//ownIP = InetAddress.getLoopbackAddress();
//System.out.println(ownIP);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Thank you sir! it J’s much appreciated!
alguien sabe mascara de subred
codigo
Thanks 😀
This doesn’t work on some version of Linux, Debian, Ubuntu, etc.
You would have to enumerate over the interfaces.
i dont want the ip address of the my website host.. i want the ip address of my website user’s pc.. please do help..!
Hey did u find answer I also need the same but in struts 2 action….
yes its working,how to congigure 3 pc in an office using java
Hi
Only return localhost IP
Current IP address : 127.0.1.1
It looks like your jave code is forced to use IPv4.
Even i have the same problem!!!!!! Need Help
final StringBuilder address = new StringBuilder(15); final TreeMap<Integer, java.net.NetworkInterface> sortedInterfaces = new TreeMap<Integer, java.net.NetworkInterface>(); for (java.net.NetworkInterface networkInterface : Collections.list(java.net.NetworkInterface.getNetworkInterfaces())) { sortedInterfaces.put(networkInterface.getIndex(), networkInterface); } for (final java.net.NetworkInterface networkInterface : sortedInterfaces.values()) { if (networkInterface.isLoopback()) continue; for (final java.net.InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { final java.net.InetAddress inetAddress = interfaceAddress.getAddress(); if (inetAddress instanceof java.net.Inet4Address) { final java.net.Inet4Address inet4Address = (java.net.Inet4Address) inetAddress; final byte[] ipAddress = inet4Address.getAddress().clone(); if (ipAddress.length != 4) continue; for (final byte ipAddressElement : ipAddress) { if (address.length() > 0) address.append('.'); address.append(String.format("%03d", ipAddressElement & 0xff)); } break; } } if (address.length() != 0) break; } if (address.length() != 15) address.delete(0, address.length());