我正在尝试使用 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);
}
}
原文由 Ashton Wilkins 发布,翻译遵循 CC BY-SA 4.0 许可协议
就我而言(有点不相关,但值得一提),我将其添加到我的 maven pom 中,错误消息消失了:
或者如果您在单元测试期间仅使用 h2: