Java – find location using Ip Address
In this example, we show you how to find a location (country, city, latitude, longitude) using an IP address.
1. GeoLite Database
The MaxMind provides a free GeoLite database (IP Address to Location).
2. GeoLite Java Example
An example to use GeoIP client Java APIs to find a location using IP address.
GetLocationExample.java
package com.mkyong.analysis.location;
import java.io.File;
import java.io.IOException;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import com.mkyong.analysis.location.mode.ServerLocation;
public class GetLocationExample {
public static void main(String[] args) {
GetLocationExample obj = new GetLocationExample();
ServerLocation location = obj.getLocation("206.190.36.45");
System.out.println(location);
}
public ServerLocation getLocation(String ipAddress) {
File file = new File(
"C:\\resources\\location\\GeoLiteCity.dat");
return getLocation(ipAddress, file);
}
public ServerLocation getLocation(String ipAddress, File file) {
ServerLocation serverLocation = null;
try {
serverLocation = new ServerLocation();
LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE);
Location locationServices = lookup.getLocation(ipAddress);
serverLocation.setCountryCode(locationServices.countryCode);
serverLocation.setCountryName(locationServices.countryName);
serverLocation.setRegion(locationServices.region);
serverLocation.setRegionName(regionName.regionNameByCode(
locationServices.countryCode, locationServices.region));
serverLocation.setCity(locationServices.city);
serverLocation.setPostalCode(locationServices.postalCode);
serverLocation.setLatitude(String.valueOf(locationServices.latitude));
serverLocation.setLongitude(String.valueOf(locationServices.longitude));
} catch (IOException e) {
System.err.println(e.getMessage());
}
return serverLocation;
}
}
Output
ServerLocation [countryCode=US, countryName=United States, region=CA,
regionName=California, city=Sunnyvale, postalCode=94089,
latitude=37.424896, longitude=-122.0074]
Can anyone provide link to get jar to get import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName; classes
where is this file
import com.mkyong.analysis.location.mode.ServerLocation;
hi, i did the code by downloading both jar file and dat file. but on running i got error as:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 31162416
at com.maxmind.geoip.LookupService.seekCountry(LookupService.java:851)
at com.maxmind.geoip.LookupService.getLocation(LookupService.java:709)
at com.maxmind.geoip.LookupService.getLocation(LookupService.java:534)
at com.maxmind.geoip.LookupService.getLocation(LookupService.java:548)
.please give me a suggession . thanks
Use GeoLiteCity.dat database instead of the one for the country
Thank you..
please send me this file GeoLiteCity.dat
GeoLiteCity.dat I need this file , i cannot fine this file in .dat format please send me in my email. i shall be thankfull to you.
Hi ,
I am getting null pointer exception while using another IP .
My Code:
try {
GetLocationExample obj = new GetLocationExample();
ServerLocation location = obj.getLocation(“192.168.1.105”);
System.out.println(location);
Class :ServerLocation
} catch (Exception ex) {
ex.printStackTrace();
}
Class : GetLocationExample
package com.action;
import java.io.File;
import java.io.IOException;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import com.action.ServerLocation;
public class GetLocationExample
{
public ServerLocation getLocation(String ipAddress)
{
File file = new File(
“C:/resources/location/GeoLiteCity.dat”);
return getLocation(ipAddress, file);
}
public ServerLocation getLocation(String ipAddress, File file) {
ServerLocation serverLocation = null;
try {
serverLocation = new ServerLocation();
LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE);
Location locationServices = lookup.getLocation(ipAddress);
serverLocation.setCountryCode(locationServices.countryCode);
serverLocation.setCountryName(locationServices.countryName);
serverLocation.setRegion(locationServices.region);
serverLocation.setRegionName(regionName.regionNameByCode(
locationServices.countryCode, locationServices.region));
serverLocation.setCity(locationServices.city);
serverLocation.setPostalCode(locationServices.postalCode);
serverLocation.setLatitude(String.valueOf(locationServices.latitude));
serverLocation.setLongitude(String.valueOf(locationServices.longitude));
} catch (IOException e) {
System.err.println(e.getMessage());
}
return serverLocation;
}
}
package com.action;
public class ServerLocation {
private String countryCode;
private String countryName;
private String region;
private String regionName;
private String city;
private String postalCode;
private String latitude;
private String longitude;
@Override
public String toString() {
return city + ” ” + postalCode + “, ” + regionName + ” (” + region
+ “), ” + countryName + ” (” + countryCode + “) ” + latitude
+ “,” + longitude;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
}
Exception :
java.lang.NullPointerException
at com.action.GetLocationExample.getLocation(GetLocationExample.java:25)
at com.action.GetLocationExample.getLocation(GetLocationExample.java:15)
at org.apache.jsp.nearme_jsp._jspService(nearme_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Hi , Do i need to download the GeoLite database to implement this in my application . Is there a process to include this in your desktop application ?
Where we can download fresh new GeoLiteCity.dat? Is it maintained by someone?
Please add a download link to get this example project
I need the GeoLiteCity.dat file. A link please
ipgeolocation.io is another IP geolocation API service that sounds a good alternative to Maxmind geoip location service.
You can also use IP geolocation API Java SDK. In the following link, you will go through the basic steps to use IP Geolocation API Java SDK.
Link: https://ipgeolocation.io/documentation/ip-geolocation-api-java-sdk-20180807094025
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
These 3 classes are not available in Jar ??
I am getting error for below imports
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName; classes
com.mkyong.analysis.location.mode.ServerLocation what is jar file this?/
Try this code This is what you are looking for, it is a class file and not contain in any jar file.
package com.mkyong.analysis.location.mode;
/**
* ServerLocation
*/
public class ServerLocation {
private String countryCode;
private String countryName;
private String region;
private String regionName;
private String city;
private String postalCode;
private String latitude;
private String longitude;
@Override
public String toString() {
return city + ” ” + postalCode + “, ” + regionName + ” (” + region
+ “), ” + countryName + ” (” + countryCode + “) ” + latitude
+ “,” + longitude;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
}
Hi
im getting LookupService Class error
please give me a suggession
thanks
It is very helpful blog.It is Working for me.
I want to know If there is another database that i can use which is more accurate then this then it will be very grateful of you.
Thanks.
Hi, you can use https://geo.ipify.org for a more accurate database.
File file = new File(
“C:\resources\location\GeoLiteCity.dat”);
How to include the file if i have hosted the file in amazon S3?
I tried with uri and url.getpath() etc. put it didn’t work. Any guesses?
import com.mkyong.analysis.location.mode.ServerLocation; please provide jar file
How can you have the library com.mkyong.analysis.location.mode.ServerLocation;?
locationServices.region and locationServices.city occurs null pointer exception.
locationServices.city and locationServices.region occurs null pointer exception.
wonderful website. This website cleared by my all doubt and simply understand than the other website. and
MKYong, quck note that you have a grammatical mistake.
Start code it should be Start coding or Start coding it
Stasio Mod.. Spell check.. It’s “quick” ,not “quck” ..lol.. 🙂
import com.mkyong.analysis.location.mode.ServerLocation;???? where is the file???
https://github.com/Albaniusz/java_mkyong/blob/master/src/main/java/com/mkyong/analysis/location/mode/ServerLocation.java
404 not found!!!!!!!!!!!!!
locationServices.countryCode occurs null pointer exception.
what should I write at C:\resources\location\GeoLiteCity.dat file?
I’m getting
java.io.IOException: Negative seek offset
error.
Use below class if you got error
package com.testipaddress.sitereference;
public class ServerLocation {
public void setCountryCode(String countryCode) {
System.out.println(“CountryCode:”+countryCode);
}
public void setCountryName(String countryName) {
System.out.println(“CountryName:”+countryName);
}
public void setRegion(String region) {
System.out.println(“Region:”+region);
}
public void setRegionName(String regionNameByCode) {
System.out.println(“RegionName:”+regionNameByCode);
}
public void setCity(String city) {
System.out.println(“City:”+city);
}
public void setPostalCode(String postalCode) {
System.out.println(“PostalCode:”+postalCode);
}
public void setLatitude(String valueOf) {
System.out.println(“Latitude:”+valueOf);
}
public void setLongitude(String valueOf) {
System.out.println(“Longitude:”+valueOf);
}
}
same error i m having..could locate serverlocation
Thanks a lott…very good example. ServerLocation is just a Simple Java class Venky with class members CountryCode,CountryName,PostalCode etc…
im getting serverLocation Class file error how i fix,
The above
1. Get a free GeoLite free Databases – here
2.Get a GeoIP client Java APIs – here
which downloads i have to download and configure
a)for this “import com.maxmind.geoip” i added jar file this import no errors
b)serverLocation class file only problem how i fix and which file confiugure i missed
in the above site so many downloads are there
Here You can find the file on web: https://github.com/Albaniusz/java_mkyong/blob/master/src/main/java/com/mkyong/analysis/location/mode/ServerLocation.java
Hi,
I’m trying to run this code using GeoLiteCity.dat and the dependency jars but I’m getting a null pointer exception.
Can anyone explain why this is happenning so that I can correct or even better explain how the location is being retrieved.
Thanks in advance
My Code:
try {
GetLocationExample obj = new GetLocationExample();
ServerLocation location = obj.getLocation(“192.168.1.105″);
System.out.println(location);
} catch (Exception ex) {
ex.printStackTrace();
}
ServerLocation Class:
package com.action;
public class ServerLocation {
private String countryCode;
private String countryName;
private String region;
private String regionName;
private String city;
private String postalCode;
private String latitude;
private String longitude;
@Override
public String toString() {
return city + ” ” + postalCode + “, ” + regionName + ” (” + region
+ “), ” + countryName + ” (” + countryCode + “) ” + latitude
+ “,” + longitude;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
}
GetLocationExample Class:
package com.action;
import java.io.File;
import java.io.IOException;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import com.action.ServerLocation;
public class GetLocationExample
{
public ServerLocation getLocation(String ipAddress)
{
File file = new File(
“C:/resources/location/GeoLiteCity.dat”);
return getLocation(ipAddress, file);
}
public ServerLocation getLocation(String ipAddress, File file) {
ServerLocation serverLocation = null;
try {
serverLocation = new ServerLocation();
LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE);
Location locationServices = lookup.getLocation(ipAddress);
serverLocation.setCountryCode(locationServices.countryCode);
serverLocation.setCountryName(locationServices.countryName);
serverLocation.setRegion(locationServices.region);
serverLocation.setRegionName(regionName.regionNameByCode(
locationServices.countryCode, locationServices.region));
serverLocation.setCity(locationServices.city);
serverLocation.setPostalCode(locationServices.postalCode);
serverLocation.setLatitude(String.valueOf(locationServices.latitude));
serverLocation.setLongitude(String.valueOf(locationServices.longitude));
} catch (IOException e) {
System.err.println(e.getMessage());
}
return serverLocation;
}
}
Error :
java.lang.NullPointerException
at com.action.GetLocationExample.getLocation(GetLocationExample.java:25)
at com.action.GetLocationExample.getLocation(GetLocationExample.java:15)
at org.apache.jsp.nearme_jsp._jspService(nearme_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Note :
Its working fine for mention IP Address:
206.190.36.45
Please suggest where i am wrong. Its very urgent.
GeoLiteCity.dat I need this file , i cannot fine this file in .dat format please send me in my email. i shall be thankfull to you.