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

41 comments on “Java – find location using Ip Address

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

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

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

    These 3 classes are not available in Jar ??

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

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

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

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

    Reply
    1. 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;
      }
      }

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

    Reply
  9. 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 ?

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

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

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

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

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

        Reply
  13. 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?

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

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

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

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

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

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

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

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

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

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

      Reply
  22. locationServices.countryCode occurs null pointer exception.

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

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

    error.

    Reply
  24. 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);

    }

    }

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

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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *