0

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.

  • Is your jar running H2 itself, or is it connecting to another process via JDBC or something? – Chris Martin Oct 27 '14 at 03:55
  • @ChrisMartin The .jar contains a class "AcmeTrigger" which extends org.h2.api.Trigger. According to H2 documentation you indicate that the file should be called every time it runs by running the SQL statement I posted above. The SQL statement returns that the class isn't found in the classpath. – AmericanKryptonite Oct 27 '14 at 04:00
  • possible duplicate of [Cannot call trigger in H2 DB](http://stackoverflow.com/questions/14124099/cannot-call-trigger-in-h2-db) – Chris Martin Oct 27 '14 at 04:12
  • @ChrisMartin Unfortunately the answer there simply states to add the classes to the classpath but that doesn't seem to be working. I'm unsure if I'm using the classpath incorrectly, because when using -verbose, I never see my class get loaded by H2. – AmericanKryptonite Oct 27 '14 at 04:15
  • Ah, sorry, I didn't notice where you included the command you're using to launch H2. – Chris Martin Oct 27 '14 at 04:18
  • Perhaps this is more relevant: http://stackoverflow.com/a/219801/402884 – Chris Martin Oct 27 '14 at 04:23
  • `AcmeTrigger` is not in a package? – Thomas Mueller Oct 27 '14 at 09:38
  • @ThomasMueller Originally I didn't have it in a package but then I did add it to a package and also added it to a jar for testing but that didn't help, I still get the same error. – AmericanKryptonite Oct 27 '14 at 14:21
  • If it's in a package, then you need to specify the package name in in the `create trigger` statement. For example `FOR EACH ROW CALL "com.acme.AcmeTrigger"`. – Thomas Mueller Oct 27 '14 at 15:35
  • I did try that, for instance my package name was alerting which I identified by using `package alerting` on the top of the source file. I then ran `jar -cvf my.jar alerting` to create a jar and then ran `FOR EACH ROW CALL "alerting.AcmeTrigger"` after adding all jars in the pwd to the classpath. – AmericanKryptonite Oct 27 '14 at 15:41

2 Answers2

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.

0

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.

lmm
  • 17,076
  • 3
  • 23
  • 37