开发者

java ClassNotFoundException for org.h2.Driver

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 m开发者_高级运维ain(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);

 }

}


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>


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.


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>


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.


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'


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.


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


Remove the scope tag

<scope>test</scope>


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


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


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.


Ran into this issue and steps to fix:

1 - Corrected this org.h2.driver to org.h2.Driver

2 - Verify that the scope of h2 dependency in pom.xml is not set to test, if that is the case, remove it so your dependency is similar to:

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

3 - Finally, I had to run mvn clean verify compile. This will refresh the classes in target folder and add the h2 class to your classpath if it is not there.

Hope it helps :)


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜