java ClassNotFoundException for org.h2.Driver

Viewed 98471

I am trying to use H2 to connect to a database in Java (using Eclipse as the IDE). The sample does (below) throws a ClassNotFoundException. The thing is, I did add the h2 jar file to the system CLASSPATH. I have even checked it's there several times via printenv in the console. Am I omitting a step?

CODE:

import java.sql.*;

public class Program {

 /**
  * @param args
  */
 public static void main(String[] args) 
  throws Exception{

  try{
   System.out.println("hello, world!");
   Class.forName("org.h2.Driver");
   Connection conn = DriverManager.getConnection("jdbc:h2:~/testdb", "sa", "");
   // add application code here
   conn.close();
  }catch(ClassNotFoundException ex){
   System.out.println( "ERROR: Class not found: " + ex.getMessage() );

  }
  System.exit(0);

 }

}
15 Answers

I was having the following error (using Intellij)

java ClassNotFoundException for org.h2.Driver

Solved it by removing the scope from my pom.

was:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <scope>test</scope>
    </dependency>

changed to:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
    </dependency>

This type of error will come when we are implementing Maven Quickstart project as a dependency to another project. Mostly occurs as test only for junit. So in application it will not work.

In my case(I use sbt) change

libraryDependencies += "com.h2database" % "h2" % "1.4.196" % Test

to

libraryDependencies += "com.h2database" % "h2" % "1.4.196"

If you use Gradle change dependency in build.gradle:

testCompile group: 'com.h2database', name: 'h2', version: '1.4.199'

to

compile group: 'com.h2database', name: 'h2', version: '1.4.199'

Use release version.

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>

In my case it's actually the connection string issue. I saw this.

After I added the mem in the URL string below, and it worked.

String url = "jdbc:h2:mem:~/test";

I have faced same issue when using : 2 times in h2 database, because Driver manager can not identify proper connection type

  • when you use "jdbc:h2:localhost:123/db", then it divide into "jdbc","h2:localhost","123/db",
  • so here expected value is h2 but getting h2:localhost because of issue in core regex for identify driver class from list of loaded drivers

Use full path like:

  • Don't: "jdbc:h2:testdb",         Do:"jdbc:h2:/c:/.../testdb"
  • Don't: "jdbc:h2:localhost:123/db",    Do: "jdbc:h2://localhost:123/db"

I am working with intelliJ. I had the "h2-1.4.200" in the lib folder. I tried every suggestion, ranging from , to . Strangely, my problem got solved only by going to these places : Project Structure -> Artifact -> Output Layout -> Available Elements and then expanding the content of the folder and then right click on "h2-1.4.200" and finally select "Extract Into Output Root". :) The strange solution

I use sbt. In build.sbt, I removed the "h2" dependency and included this instead: "com.h2.database" % "h2" % "1.4.200" And it worked!

This worked with testRuntimeOnly 'com.h2database:h2:1.4.200'

and in application.yaml property remove or comment out # driverClassName: org:h2:Driver

As per springboot latest documentation jdbc driver is not required as it is automatically detected.

Remove the scope tag

<scope>test</scope>
Related