title: How to efficiently initializing a large, static data table with Tomcat
body: <p>I am starting with Java/Tomcat, and I am struggling with a problem that was very easy to solve with C++.</p>
  
  <p>My webservice (sigle webapp) works by using the input values to lookup the numeric answer in a large, pre-calculated table.  I am struggling with the initialization of this table.</p>
  
  <p>Problem details: </p>
  
  <ul>
  <li>The data table is huge (3000x3000);</li>
  <li>The data is pre-computed and this computation is very costly (it
  takes hours);</li>
  <li>The data is static, it will never change after it is calculated for a
  given instance;</li>
  </ul>
  
  <p>In C++, I would just define a a static const array and initialize it inline.  I was not able to do this in Java, apparently there's no concept of static data initialization in Java, it needs to generate initialization code and this code cannot be larger than 64k.  In fact, I couldn't even load the file with the static initialization in Eclipse, it would hang-up.</p>
  
  <p>So I need to initialize the table from a static file on disk.  I tried to place a .csv file on WEB-INF/static, but found no way to open it reliably from inside my Java code (the absolute path will be in different places on my development and production environments, for example).</p>
  
  <p>This is my current class definition (with mocked-up data for the initialization):</p>
  
  <pre><code>package com.hmt.restjersey;
  
  public final class G {
      static public final float[][] data = new float[3000][3000];
  
      //TODO: actual initialization from file
      static {
          Logger.writeEventLog("Initializing G table...");
  
          for (int alpha = 0; alpha &lt; 3000; alpha++) {
              for (int beta = 0; beta &lt; 3000; beta++) {
                  data[alpha][beta] = 1.0f / (1 + alpha + beta);
              }
          }
          Logger.writeEventLog("G table initialized.");
      }
  }
  </code></pre>
  
  <p>So, my questions:</p>
  
  <ul>
  <li>How to reliably access the data file (WEB-INF/static/data.csv) to initialize the table?</li>
  <li>Is a .csv file the best way to load numeric data efficiently?</li>
  </ul>
  
  <p>Also, since the table is huge I would like to have a single instance of it in the server to save memory and speed up initialization.  How do I assure that there will be a single instance shared by all servlet processes?</p>