java ClassNotFoundException for org.h2.Driver

前端 未结 13 2371
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 10:40

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

相关标签:
13条回答
  • 2021-01-01 11:07

    Use release version.

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    
    0 讨论(0)
  • 2021-01-01 11:10

    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>
    
    0 讨论(0)
  • 2021-01-01 11:12

    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.

    0 讨论(0)
  • 2021-01-01 11:13

    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.

    0 讨论(0)
  • 2021-01-01 11:14

    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

    0 讨论(0)
  • 2021-01-01 11:20

    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"
    0 讨论(0)
提交回复
热议问题