title: Adding aggregate function to external H2 database
body: <p>I'm trying to create an aggregate function in my H2 database using Java.
  The function should return a custom median calculation from the given Double column.
  This calculation consist in using only the values that are close enough to the average value using the <em>max_variance</em> int value. For this I created the class:</p>
  
  <pre><code>package custommedian;
  
  public class CustomMedian implements org.h2.api.AggregateFunction{
      final int max_variance = 7;
      java.util.LinkedList&lt;Double&gt; values = new java.util.LinkedList&lt;Double&gt;();
  
      @Override
      public void init(java.sql.Connection cnctn) throws java.sql.SQLException {
          // I ignored this
      }
  
      @Override
      public int getType(int[] ints) throws java.sql.SQLException {
         return java.sql.Types.DOUBLE;
      }
  
      @Override
      public void add(Object o) throws java.sql.SQLException {
          values.add((Double)o);
      }
  
      @Override
      public Object getResult() throws java.sql.SQLException {
          double average = 0;
          java.util.Iterator&lt;Double&gt; i;
          java.util.LinkedList&lt;Double&gt; properValues = new java.util.LinkedList&lt;Double&gt;();
  
          // Get average value
          for( i = values.iterator(); i.hasNext(); ) {
              average += i.next();    
          }
          average = average / values.size();
  
          // Filter out invalid values
          for( i = values.iterator(); i.hasNext(); ) {
              double value = i.next();
              if (value - max_variance &lt; average &amp;&amp; value + max_variance &gt; average){
                  properValues.add(value);
              }
          }
  
          // Sort list
          java.util.Collections.sort(properValues);
  
          // Return median
          int size = properValues.size();
          if (size &gt; 0){
              int pos = ((int) size/2);
              // Odd size
              if ((size%2) == 1 ) return properValues.get(pos);
              // Even size
              else return ( properValues.get(pos-1) + properValues.get(pos) ) / 2;
          }
          else 
              return null;
      }
  }
  </code></pre>
  
  <p>And now I'm supposed to compile it so it's accessible for the database and use this command:</p>
  
  <pre><code>CREATE AGGREGATE MEDIAN FOR "custommedian.CustomMedian";
  </code></pre>
  
  <p>How am I supposed to do that? </p>
  
  <p>I tried putting the .jar file in the same folder as the database file is but it doesn't seem to find the class:</p>
  
  <pre><code>Class "custommedian.CustomMedian" not found; SQL statement:
  </code></pre>
  
  <p>Anyone could please tell me what else I have to do to make this work?</p>
  
  <p>Thanks a lot in advance!</p>
  
  <p>EDIT: </p>
  
  <p>Solved:</p>
  
  <p>As Lukas suggested, I ended up embedding the H2 jar into my application and this way there are no classpath issues.</p>