org.h2.Driver的java ClassNotFoundException [英] java ClassNotFoundException for org.h2.Driver

查看:99
本文介绍了org.h2.Driver的java ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

代码:

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

 }

}


推荐答案

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

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>

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

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>

这篇关于org.h2.Driver的java ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆