Exception in thread “main” java.lang.ClassNotFoundException: org.h2.Driver

岁酱吖の 提交于 2019-12-11 18:08:29

问题


I am working on a dB viewer for a h2 database in java.

I am using a sample code to try and connect to it as follows:

import java.sql.*;
public class db {
    public static void main(String[] a)
            throws Exception {
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.
            getConnection("jdbc:h2:~/test", "sa", "");
        // add application code here
        conn.close();
    }
}

But when I run it I get the following error:

Exception in thread "main" java.lang.ClassNotFoundException: org.h2.Driver at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at db.main(db.java:5)

Any ideas? I have installed h2 using the installer.


回答1:


Add the h2 jdbc driver to your classpath or as library to your project if you using an IDE.




回答2:


Simply, you have not correctly added the h2 database as a dependency of your Java project.

  • If you're using Eclipse you may use Rightclick on Project > Properties > Java Build Path > Add External Jars. Select the h2 database library JAR file.


来源:https://stackoverflow.com/questions/29609985/exception-in-thread-main-java-lang-classnotfoundexception-org-h2-driver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!