How do you get Intellij/Gradle/Kotlin/H2 running?
So frustrated... All I can get is "ClassNotFoundException" trying to load forName "org.h2.Driver"
Trying to get an H2 file database working in a java project managed by IntelliJ with a gradle build system using kotlin. All I can get is a "ClassNotFoundException" thrown and every single post offering a solution refers to "compile group" in the dependencies in build.gradle to get the h2 jar on the classpath. "compile" is deprecated/replaced by testimplementation and the rest of the suggestion with group doesn't match kotlin syntax. It seems nobody else is using/building H2 with the combination of gradle/kotlin/H2, at least not recent versions of these.
Here's what I've got... maybe someone can clue me in on what I've done wrong/missing... super basic new project:
build.gradle:
plugins {
id("java")
id("application")
}
group = "org.example"
version = "1.0-SNAPSHOT"
application {
mainClass.set("org.example.Main")
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
testImplementation("com.h2database:h2:2.1.214")
}
tasks.getByName<Test>("test") {
useJUnitPlatform()
}
My Main class is minimal:
package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
try {
Class.forName("org.h2.Driver");
Connection c = DriverManager.getConnection("jdbc:h2:~/test", "", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
I've added "Gradle: com.h2database:h2:2.1.214" as a library in the Modules under Project Settings for both "test" and "main".
I created a simple Run Configuration of type "Application" "java 17" "-cp HelpH2.main" "org.example.Main"
I also created a gradle Run Configuration with task set to 'run' that doesn't make a difference either.
Everything compiles but when I run it (either with the application configuration or the gradle run task)...
BUILD SUCCESSFUL in 157ms
2 actionable tasks: 1 executed, 1 up-to-date
java.lang.ClassNotFoundException: org.h2.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at org.example.Main.main(Main.java:12)
4:16:48 PM: Execution finished ':Main.main()'.
Quickly driving me mad and wondering if it's Intellij, whether it's gradle, Kotlin??
From what I've read, and the clearly faulty understanding I've built from it... The class isn't found because it isn't specifically referenced in the compile (since java.sql is (not org.h2) and thus needs to be on the classpath to be found. (which starts me wanting to interrogate the designers of the whole "java.sql" methodology and the nightmare that is java packaging in general.) But anyways, from reading... "testimplementation" in the build.gradle file should put the specified dependency on both the compile and runtime classpaths. So, it should be there anyways.
Can anybody help? I'm at a loss and quickly thinking of just going back to downloading jar files, dumping them in a folder and just using ant builds instead of these "better" solutions.
In short... I'm quickly realizing why python became so popular
请先登录再写评论。
Oh... my final goal is a JavaFX application. But IntelliJ doesn't offer/support kotlin for JavaFX projects. But it doesn't matter. Even using the groovy based setup everything still acts identically and I can't figure out how to get org.h2.Driver class loaded.
Try defining H2 dependency like that:
dependencies {implementation("com.h2database:h2:2.1.212")
}
Share a sample project zipped via https://uploads.jetbrains.com/ if it does not help