Java-based H2 Database cannot locate items in CLAS

2019-09-06 08:58发布

问题:

I have a scala file has been compiled with the following command:

scalac -cp ".:*" AcmeTrigger.scala

In the directory with the .scala file I have some .jar files that contain the APIs for email and texting services that I'm using. No issues here.

The scala file essentially sends text messages and emails when someone modifies a table in a database. I start the database with the following command:

java -cp ".:*" -jar h2-1.4.182.jar

Essentially telling it to use the the .class and .jar files in directory and add them to the classpath. I've done many variations of this. Without .class, with ".:*" with the fully typed name. No matter what I do, H2 doesn't recognize the class.

So there's an operation in this database that connects the database to a class:

CREATE TRIGGER ALERT AFTER INSERT ON "event" FOR EACH ROW CALL "AcmeTrigger"

When I run this I get an error that states it cannot find the class:

Error creating or initializing trigger "ALERT" object, class "AcmeTrigger", cause: "org.h2.message.DbException: Class ""AcmeTrigger"" not found [90086-182]"

I don't think the error is in the query syntax, but with how I'm using classpath's and was hoping someone with more Java/Scala experience could help me here. I've tried many variations of starting the .jar file but nothing seems to help.

回答1:

There's something about the default main method of the H2 jar that results in strange class loader behavior (more research needed to pinpoint what). I had the exact same experience as OP.

After copying the compiled class into your directory of choice (e.g. libs), as well as the H2 jar, run the H2 server like this:

java -cp "/libs:/libs/h2-1.4.197.jar" org.h2.tools.Server

This should result in correct resolution of the class referenced by the trigger.



回答2:

AFAIK you can't add individual .class files to the classpath. Try java -cp ".:*.jar" (i.e. use this directory as an entry on the classpath, not the individual .class files)

Also note that scalac implicitly includes e.g. the scala library on the classpath. Make sure you have the appropriate scala-library.jar.