I am starting the H2 Server Tool from Command Line as:
java -cp h2*.jar org.h2.tools.Server
And I am trying to use a user defined class like the Function
one shown in the User-Defined Functions and Stored Procedures section.
I have made several attempts with my user function:
Function.class
fileacme/Function.class
fileacme.jar
fileh2acme.jar
fileAnd tried several ways of including it to the classpath, with no luck.
For example:
[user@host h2]$ java -cp ./h2*.jar:./acme.jar org.h2.tools.Server -tcp -web
Error: Could not find or load main class org.h2.tools.Server
I have read this related issue and followed the indications there.
But it does not seem to work.
The only way I was able to make this work is by updating h2*.jar
file with my user defined class:
[user@host h2]$ jar uf h2-1.4.197.jar acme/Function.class
[user@host h2]$ jar tvf h2-1.4.197.jar | grep acme
421 Wed Sep 26 12:17:16 EEST 2018 acme/Function.class
And then from the H2 Shell:
[user@host h2]$ java -cp h2*.jar org.h2.tools.Shell -user sa -password sa -url jdbc:h2:tcp://localhost/~/mydb
Welcome to H2 Shell 1.4.197 (2018-03-18)
...
sql> CREATE ALIAS IS_PRIME FOR "acme.Function.isPrime";
(Update count: 0, 9 ms)
sql> CALL IS_PRIME(5);
PUBLIC.IS_PRIME(5)
TRUE
(1 row, 31 ms)
Though I did want to keep my user defined functions in a separated jar.
If anyone knows how to do it, it would be appreciated.
I got H2 server to use my Java & Kotlin functions in a user defined function. There are several steps that must be done correctly for it to work.
First, is to build the jar with your functions (I had both a Java and Kotlin functions). After several error messages from H2, when it tried to compile and run my functions, I realized that including my project's standard jar was not enough. I had to include all its dependencies. Luckily, Gradle and Maven can make this kind of fat jar. I used the maven-assembly-plugin
to make mine.
Second, I told H2 where to find the H2 jar and my fat jar in my local Maven repo.
H2_JAR=/Users/me/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar
MY_JAR=/Users/me/.m2/repository/my/foo/app/1.0-SNAPSHOT/myfooapp-1.0-SNAPSHOT-jar-with-dependencies.jar
java -cp "$H2_JAR:$MY_JAR" org.h2.tools.Server -tcpPort 27777 -tcp
FYI: If you want H2 to call a Kotlin function, then the Kotlin standard lib must be in the fat jar. (The maven-assembly-plugin
will include it.). Also, your Kotlin function will need to be in a companion object
. I also added the @JvmStatic
annotation to that Kotlin function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.