How to get Server IP address in Java
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