org.h2.Driver 的 java ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4008011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:15:37  来源:igfitidea点击:

java ClassNotFoundException for org.h2.Driver

javajdbch2classnotfoundexception

提问by Ashton Wilkins

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

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

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

 }

}

回答by Pascal Thivent

The sample does (below) throws a ClassNotFoundException

该示例确实(如下)抛出 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.

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

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 CLASSPATHenvironment variable is a bad practice anyway and you should use the -cpoption if you're running Java on the command line. Like this:

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

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?

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

Yes. Under Eclipse, add the JAR to the project build path: right-clickon 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.

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

回答by rogerdpack

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

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

  <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:

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

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

回答by naXa

Recently I encountered the java.lang.ClassNotFoundException: org.h2.Driverexception in IntelliJ IDEA 2017.2 EAPwhile using the latest version (1.4.196) of H2driver. The solution was to downgrade to 1.4.195 that worked.

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

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

回答by Dhiraj Gandhi

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.

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

回答by Xianhong Xu

In my case(I use sbt) change

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

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

to

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

回答by Aman

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.

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

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

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

回答by Vahe Gharibyan

Use release version.

使用发布版本。

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

回答by bleh10

I was having the following error (using Intellij)

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

java ClassNotFoundException for org.h2.Driver

org.h2.Driver 的 java ClassNotFoundException

Solved it by removing the scope from my pom.

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

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.

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

回答by uudaddy

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

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

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

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

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

回答by Sanchez

If you use Gradle change dependency in build.gradle:

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

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

to

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