java ClassNotFoundException for org.h2.Driver

2019-02-11 12:25发布

问题:

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);

 }

}

回答1:

In my case (unrelated a bit, but worth mentioning), I added this to my maven pom, and the error message went away:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>xxx</version> <!-- ex: 1.2.140 -->
  </dependency>

or if you are only using h2 during unit testing:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>xxx</version> <!-- ex: 1.2.140 -->
    <scope>test</scope>
  </dependency>


回答2:

Recently I encountered the java.lang.ClassNotFoundException: org.h2.Driver exception in IntelliJ IDEA 2017.2 EAP while using the latest version (1.4.196) of H2 driver. The solution was to downgrade to 1.4.195 that worked.

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


回答3:

The sample does (below) throws a ClassNotFoundException

Then the driver is not on the classpath.

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.

How did you do that exactly? Please show the obtained output.

Am I omitting a step?

I can't say with the provided informations. But relying on the CLASSPATH environment variable is a bad practice anyway and you should use the -cp option if you're running Java on the command line. Like this:

java -cp h2.jar com.acme.Program

Is there a way I can set Eclipse to use the jar file when I use the RUN menu so that I don't have to run from the Console all the time?

Yes. Under Eclipse, add the JAR to the project build path: right-click on your project then Properties > Java Build Path > Libaries > Add JARS... (assuming the H2 JAR is available in a directory relative to your project). Others IDE have equivalent way of doing this.



回答4:

The <scope>[database_name]</scope> should include the database you are working with. If you change your db from one to another, make sure to change the scope also. As soon as i changed it the error went away.



回答5:

In my case(I use sbt) change

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

to

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


回答6:

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>


回答7:

Use release version.

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


回答8:

In my case it's actually the connection string issue. I saw this mentioned somewhere over the web: e.g., https://dzone.com/articles/working-with-embedded-java-databases-h2-amp-intell

After I added the "mem" in the URL string below, it worked. I probably spent 2 hours on this. String url = "jdbc:h2:mem:~/test";



回答9:

Using <scope>test</scope> should not work logically. try it with <scope>runtime</scope> or <scope>provided</scope>, unless you need it only for testing phase.

On maven docs, it says that <scope>test</scope> dependency is not required for normal use of the application, and is only available for the test compilation and execution phases
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html