java.lang.ClassNotFoundException:来自xerial的Sample.java程序中的org.sqlite.JDBC错误

我试图让Xerial的Sample类在Eclipse中使用sqlite,但我不断收到错误“ClassNotFoundException:org.sqlite.JDBC”

我从https://bitbucket.org/xerial/sqlite-jdbc/downloads下载了sqlite-jdbc-3.7.2.jar文件。 将它复制到eclipse中我的项目“database_test”下的lib文件夹中。 然后右键单击Project-> Properties-> Java Build Path-> Libraries选项卡 – > Add JARs->选择jar文件。 我试图从这里找到的Xerial执行此代码: https ://bitbucket.org/xerial/sqlite-jdbc#markdown-header-usage

// load the sqlite-JDBC driver using the current class loader Class.forName("org.sqlite.JDBC"); Connection connection = null; try { // create a database connection connection = DriverManager.getConnection("jdbc:sqlite:sample.db"); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. statement.executeUpdate("drop table if exists person"); statement.executeUpdate("create table person (id integer, name string)"); statement.executeUpdate("insert into person values(1, 'leo')"); statement.executeUpdate("insert into person values(2, 'yui')"); ResultSet rs = statement.executeQuery("select * from person"); while(rs.next()) { // read the result set System.out.println("name = " + rs.getString("name")); System.out.println("id = " + rs.getInt("id")); } } catch(SQLException e) { // if the error message is "out of memory", // it probably means no database file is found System.err.println(e.getMessage()); } finally { try { if(connection != null) connection.close(); } catch(SQLException e) { // connection close failed. System.err.println(e); } } 

}}

我去过的每个站点都说过将jar文件添加到你的构建路径或类路径中,我相信我已经做到了,但没有解决问题。 任何帮助,将不胜感激。 谢谢。

感谢用户phew的帮助/想法。

我错过了Xerial网站上针对Sample程序的明显命令行指令。 要使程序从命令行运行,我必须将JAR文件复制到与.java程序相同的文件夹中。 然后运行以下命令:

java -classpath“。:sqlite-jdbc-(VERSION).jar”Sample

对我有用的一种方法是转到JRE系统库 – >右键clik – >构建路径 – >配置构建路径 – >添加外部jar – >选择jar并编译。

首先,您可以在项目中添加jar文件后下载sqlite-jdbc-3.8.11.2

 Window > Show_view > Package Explorer step-1: right click on referenced libraries step-2: select Built path step-3: configure Built path step-4: Add External jars file step-5: add sqlite-jdbc-3.8.11.2 step-6: ok 

或第二种方式有用


您的项目可以设置Classpath:Lib / sqlite-jdbc-3.8.11.2.jar

 1)create a folder "Lib" in your project workspace 2)"Lib" folder paste jar file like "sqlite-jdbc-3.8.11.2.jar" 3)After Double click on your project file ie 'MANIFEST.MF' 4)select Runtime(Bottom view panel) 5)plug-in Classpath(Bottom_right position) to add jar file like "Lib/sqlite-jdbc-3.8.11.2.jar" this working 100% sure..thanks 

如前所述,将jar文件添加到项目中。

对于Android Studio / IntelliJ,您只需这样:

文件>项目结构>

在此处输入图像描述

新模块>

在此处输入图像描述

导入.JAR / .AAR包

在此处输入图像描述

现在选择/添加.jar文件,让IntelliJ完成剩下的工作。

当您处理桌面JAVA应用程序时,上述所有解决方案都能正常运行。 对于WebAPP应用程序,以下解决方案将无法正常工作。 实际上这是您的App服务器的问题,即您需要在WEB-INF / lib下添加sqlite-jar,然后只有您才能成功运行您的webapp。

为此,您可以按照以下步骤操作:

去:

Project-> Properties-> Deployment Assembly-> Add-> Archives From File System – > Next – > Add

导航到您拥有sqlite-jar的文件夹,选择它并单击OK。

单击完成。 好。

完成后,您应该能够立即运行您的应用程序。

谢谢

步骤1:

复制sqlite库。


第2步:

将库粘贴到’WebContent / WEB-INF / lib’目录中,也可以通过在eclipse中选择’lib’文件夹并按Ctrl + V来完成


第3步:

重启服务器,希望能解决问题