38

我正在尝试使用H2连接到 Java 中的数据库(使用 Eclipse 作为 IDE)。该示例确实(如下)抛出了一个ClassNotFoundException. 问题是,我确实将 h2 jar 文件添加到系统 CLASSPATH 中。printenv我什至通过控制台多次检查了它的存在。我省略了一步吗?

代码:

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

 }

}
4

15 回答 15

38

在我的情况下(有点无关,但值得一提),我将它添加到我的 maven pom 中,错误消息消失了:

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

或者如果您仅在单元​​测试期间使用 h2:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>xxx</version> <!-- ex: 1.2.140 -->
    <scope>test</scope>
  </dependency>
于 2013-07-31T18:35:41.783 回答
16

最近我java.lang.ClassNotFoundException: org.h2.Driver在 IntelliJ IDEA 2017.2 EAP中使用最新版本(1.4.196)的H2驱动程序时遇到了异常。解决方案是降级到有效的 1.4.195。

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.195</version>
    <scope>test</scope>
</dependency>
于 2017-06-14T10:32:59.517 回答
14

我遇到以下错误(使用 Intellij)

org.h2.Driver 的 java ClassNotFoundException

通过从我的 pom.xml 中删除范围来解决它。

曾是:

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

变成:

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

当我们将 Maven 快速入门项目作为对另一个项目的依赖项来实现时,就会出现这种类型的错误。大多数情况下仅作为 junit 的测试。所以在应用中它不起作用。

于 2018-12-14T13:47:15.633 回答
8

该示例确实(如下)引发了 ClassNotFoundException

那么驱动程序不在类路径上。

问题是,我确实将 h2 jar 文件添加到系统 CLASSPATH 中。我什至通过控制台中的“printenv”检查了它是否存在多次。

你是怎么做到的?请显示获得的输出。

我省略了一步吗?

我不能用提供的信息说。但是无论如何,依赖CLASSPATH环境变量是一种不好的做法,-cp如果您在命令行上运行 Java,则应该使用该选项。像这样:

java -cp h2.jar com.acme.Program

有没有一种方法可以让我在使用 RUN 菜单时将 Eclipse 设置为使用 jar 文件,这样我就不必一直从控制台运行?

是的。在 Eclipse 下,将 JAR 添加到项目构建路径:右键单击您的项目,然后单击Properties > Java Build Path > Libaries > Add JARS...(假设 H2 JAR 在与您的项目相关的目录中可用)。其他 IDE 也有类似的方法。

于 2010-10-24T12:30:01.800 回答
4

在我的情况下(我使用 sbt)改变

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

libraryDependencies += "com.h2database" % "h2" % "1.4.196"
于 2018-02-08T03:08:06.810 回答
3

如果您在 build.gradle 中使用 Gradle 更改依赖项:

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

compile group: 'com.h2database', name: 'h2', version: '1.4.199'
于 2019-05-29T22:01:53.327 回答
2

<scope>[database_name]</scope>应该包括您正在使用的数据库。如果您将数据库从一个更改为另一个,请确保也更改范围。一旦我改变它,错误就消失了。

于 2017-09-19T07:52:16.833 回答
1

使用发布版本。

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
于 2018-10-14T09:49:16.883 回答
1

就我而言,这实际上是连接字符串问题。我看到了这个

在我mem在下面的 URL 字符串中添加之后,它就起作用了。

String url = "jdbc:h2:mem:~/test";
于 2019-02-09T19:48:07.290 回答
0

使用<scope>test</scope>不应该在逻辑上起作用。尝试使用<scope>runtime</scope>or <scope>provided</scope>,除非您仅在测试阶段需要它。

在 maven 文档上,它表示<scope>test</scope> 正常使用应用程序不需要依赖项,并且仅适用于测试编译和执行阶段
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism .html

于 2018-07-22T20:11:11.737 回答
0

删除范围标签

<scope>test</scope>
于 2022-03-05T17:32:20.180 回答
0

我在使用时遇到了同样的问题在 h2 数据库中 2 次,因为驱动程序管理器无法识别正确的连接类型

  • 当你使用 "jdbc:h2:localhost:123/db" 时,它分为 "jdbc","h2:localhost","123/db",
  • 所以这里的预期值是h2但得到h2:localhost因为核心正则表达式中的问题,用于从加载的驱动程序列表中识别驱动程序类

使用完整路径,例如:

  • 不要:“jdbc:h2:testdb”,要:“jdbc:h2:/c:/.../testdb”
  • 不要:“jdbc:h2:localhost:123/db”,做:“jdbc:h2://localhost:123/db”
于 2020-02-27T05:53:40.490 回答
0

我正在与 IntelliJ 合作。我在 lib 文件夹中有“h2-1.4.200”。我尝试了每一个建议,范围从 , 到 。奇怪的是,我的问题只能通过去这些地方解决:项目结构->工件->输出布局->可用元素,然后展开文件夹的内容,然后右键单击“h2-1.4.200”,最后选择“提取到输出根”。:) 奇怪的解决方案

于 2020-04-18T15:08:47.747 回答
0

这适用于 testRuntimeOnly 'com.h2database:h2:1.4.200'

并在 application.yaml 属性中删除或注释掉 # driverClassName: org:h2:Driver

根据 springboot 最新文档,不需要 jdbc 驱动程序,因为它是自动检测的。

于 2021-08-01T03:51:52.273 回答
-1

我用 sbt。在 build.sbt 中,我删除了“h2”依赖项并包含了这个:“com.h2.database”%“h2”%“1.4.200”它成功了!

于 2020-04-22T11:32:29.670 回答