title: MaxMind Get Lat/Lng from IP address, InvalidDatabaseException Error
body: <p>My end goal is to obtain the latitude, longitude and country associated with a provided ip address. I am using java and the MaxMind GeoIP2 Java API. My preference is <strong>not</strong> to use their webapi as I need quick turn around time. </p>
  
  <p>Can anyone tell me why I am getting this error when I call my function that ties in the MaxMind api call (code shown after the error)? </p>
  
  <p>I am not sure what a MaxMind DB metadata marker is. It is complaining about the file that I grabbed off of the MaxMind site. I would assume it's a valid MaxMind DB file. </p>
  
  <pre><code>com.maxmind.db.InvalidDatabaseException: Could not find a MaxMind DB metadata marker in this file (GeoLite2-City-Blocks-IPv6.csv). Is this a valid MaxMind DB file?
  at com.maxmind.db.Reader.findMetadataStart(Reader.java:231)
  at com.maxmind.db.Reader.&lt;init&gt;(Reader.java:82)
  at com.maxmind.db.Reader.&lt;init&gt;(Reader.java:74)
  at com.maxmind.geoip2.DatabaseReader.&lt;init&gt;(DatabaseReader.java:41)
  at com.maxmind.geoip2.DatabaseReader.&lt;init&gt;(DatabaseReader.java:31)
  at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:126)
  at com.tutelatechnologies.tapfortap.server.maxmind.MaxMindLookUp.maxMindLookup(MaxMindLookUp.java:20)
  at com.tutelatechnologies.tapfortap.server.cidlaclookup.T4TUtilitiesTest.testMaxMindLookUpTest(T4TUtilitiesTest.java:23)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>
  
  <p>I followed the instructions and sample code on the MaxMind site here for the Data Usage section: <a href="http://maxmind.github.io/GeoIP2-java/" rel="nofollow">http://maxmind.github.io/GeoIP2-java/</a> </p>
  
  <p>I have chosen to use a GeoLite2 csv dbs obtained from here: <a href="http://dev.maxmind.com/geoip/geoip2/geolite2/" rel="nofollow">http://dev.maxmind.com/geoip/geoip2/geolite2/</a></p>
  
  <p>My code: </p>
  
  <pre><code>public class MaxMindLookUp {
  
  public static final void maxMindLookup(final String ipaddress) {
      File db = new File(
              "src/main/java/com/tutelatechnologies/tapfortap/server/maxmind/GeoLite2-City-Blocks-IPv6.csv");
      try {
          DatabaseReader reader = new DatabaseReader.Builder(db).build();
          InetAddress ip = InetAddress.getByName(ipaddress);
  
          CityResponse response = reader.city(ip);
          Location loc = response.getLocation();
  
          Double lat = loc.getLatitude();
          Double lng = loc.getLongitude();
          Integer acc = loc.getAccuracyRadius();
  
          System.out.println("lat=" + lat + "\nlng=" + lng + "\nacc=" + acc);
  
      } catch (GeoIp2Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
  
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>Note: in order to run the code above you need to add a plugin to your Maven Dependencies section. </p>
  
  <pre><code> &lt;dependency&gt;
      &lt;groupId&gt;com.maxmind.geoip2&lt;/groupId&gt;
      &lt;artifactId&gt;geoip2&lt;/artifactId&gt;
      &lt;version&gt;2.1.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I didn't come across much for this exact error on SO. I have stepped through the debugger and the error happens on this line: </p>
  
  <pre><code>DatabaseReader reader = new DatabaseReader.Builder(db).build();
  </code></pre>
  
  <p>This leads me to believe something is wrong with the db that I downloaded off the site.</p>
  
  <p>The Exceptions heading at the bottom of this page (<a href="http://maxmind.github.io/GeoIP2-java/" rel="nofollow">http://maxmind.github.io/GeoIP2-java/</a>) provides a dead link 'GeoIP2 Precision web service documentation' so that's no help. </p>
  
  <p>There doesn't seem to be anything related to this exception in the Exceptions portion of their Java api: <a href="http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/" rel="nofollow">http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/</a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>According to the DatabaseReader.Builder().build() description in the specs here: <a href="http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/" rel="nofollow">http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/</a> I think the issue comes from reading the db. I am not sure yet how to solve this issue. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I was able to get it working using the binary db files (GeoLite2-City.mmdb). I am leaving this question open in case anyone has any more information about why the CSV db files did not work for me.</p>