Hello experts around the globe :)

I am attempting to create an aggregate function during my H2 database using Java. The function should return a custom median calculation in the given Double column. This calculation consist in making use of just the values which are close enough towards the average value while using *max_variance* int value. With this I produced the course:

package custommedian;

public class CustomMedian implements org.h2.api.AggregateFunction{
    final int max_variance = 7;
    java.util.LinkedList<Double> values = new java.util.LinkedList<Double>();

    public void init(java.sql.Connection cnctn) throws java.sql.SQLException {
        // I ignored this

    public int getType(int[] ints) throws java.sql.SQLException {
       return java.sql.Types.DOUBLE;

    public void add(Object o) throws java.sql.SQLException {

    public Object getResult() throws java.sql.SQLException {
        double average = 0;
        java.util.Iterator<Double> i;
        java.util.LinkedList<Double> properValues = new java.util.LinkedList<Double>();

        // 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 < average && value + max_variance > average){

        // Sort list

        // Return median
        int size = properValues.size();
        if (size > 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;
            return null;

And today I am designed to compile it therefore it is accessible for that database and employ this command:

CREATE AGGREGATE MEDIAN FOR "custommedian.CustomMedian";

How shall we be held supposed to achieve that?

I attempted putting the .jar file within the same folder because the database file is however it does not appear to obtain the class:

Class "custommedian.CustomMedian" not found; SQL statement:

Anybody could please let me know what else I must do to make this happen?

Thanks ahead of time!



As Lukas recommended, I wound up embedding the H2 jar into my application and by doing this you will find no classpath issues.

Perhaps you have added your class towards the classpath from the H2 database process? I.e. when setting up the H2 database?

A different way to do that would be to fork H2 and make it as well as your custom aggregate function. Or simply place your .class file within the H2 jar file, for the way frequently you need to improve your own code, this can be ok, too. It will definitely be around the classpath...

Within the worst situation, the H2 user group is very active:


When running the H2 server being an exterior process, the classes can be included to the classpath using java -cp <dir> ....

While using the h2.sh or h2.bat to begin the H2 server, you can include your directory towards the atmosphere variables %H2DRIVERS% or %CLASSPATH%.