Main Tutorials

Java – find location using Ip Address

Geolocation

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

  1. Get a free GeoLite free Databases – here
  2. Get a GeoIP client Java APIs – here
  3. Start code it.

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]

References

  1. Wikipedia : Geolocation software
  2. GeoLite Free Downloadable Databases

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
41 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
TJSE
6 years ago

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

deepak
5 years ago

where is this file
import com.mkyong.analysis.location.mode.ServerLocation;

Srijini
9 years ago

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

Shivani
9 years ago
Reply to  Srijini

Use GeoLiteCity.dat database instead of the one for the country

Justin James
9 years ago
Reply to  Shivani

Thank you..

sajjad haider
5 years ago

please send me this file GeoLiteCity.dat

sajjad haider
5 years ago

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.

Basant Kumar
6 years ago

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)

Ankur Sandilya
7 years ago

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 ?

Igor Bajovic
8 years ago

Where we can download fresh new GeoLiteCity.dat? Is it maintained by someone?

Joe
9 years ago

Please add a download link to get this example project

Jus
3 years ago

I need the GeoLiteCity.dat file. A link please

Ramiz Yaseen
4 years ago

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

Saurabh
6 years ago

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;

These 3 classes are not available in Jar ??

TJSE
6 years ago

I am getting error for below imports
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName; classes

SAJID
6 years ago

com.mkyong.analysis.location.mode.ServerLocation what is jar file this?/

Souleh Shaikh
6 years ago
Reply to  SAJID

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;
}
}

oum rouma
7 years ago

Hi
im getting LookupService Class error
please give me a suggession
thanks

Parth Prajapati
8 years ago

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.

Nag
4 years ago

Hi, you can use https://geo.ipify.org for a more accurate database.

burhan
9 years ago

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?

ashutosh
9 years ago

import com.mkyong.analysis.location.mode.ServerLocation; please provide jar file

phat
9 years ago

How can you have the library com.mkyong.analysis.location.mode.ServerLocation;?

francis
9 years ago

locationServices.region and locationServices.city occurs null pointer exception.

franko
9 years ago

locationServices.city and locationServices.region occurs null pointer exception.

Jandi
9 years ago

wonderful website. This website cleared by my all doubt and simply understand than the other website. and

Stasio Mod
9 years ago

MKYong, quck note that you have a grammatical mistake.
Start code it should be Start coding or Start coding it

Vyshak
9 years ago
Reply to  Stasio Mod

Stasio Mod.. Spell check.. It’s “quick” ,not “quck” ..lol.. 🙂

som
9 years ago

import com.mkyong.analysis.location.mode.ServerLocation;???? where is the file???

m ta
4 years ago
Reply to  Azim

404 not found!!!!!!!!!!!!!

snow
9 years ago

locationServices.countryCode occurs null pointer exception.

snow
9 years ago

what should I write at C:\resources\location\GeoLiteCity.dat file?

I’m getting
java.io.IOException: Negative seek offset

error.

udaykiranpulipati
10 years ago

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);

}

}

sachin
10 years ago

same error i m having..could locate serverlocation

sachin
10 years ago
Reply to  sachin

Thanks a lott…very good example. ServerLocation is just a Simple Java class Venky with class members CountryCode,CountryName,PostalCode etc…

venky
10 years ago

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

MAP
8 years ago

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

Basant Kumar
6 years ago
Reply to  MAP

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.

sajjad haider
5 years ago
Reply to  Basant Kumar

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.