java.lang.ClassNotFoundException:org.sqlite.JDBC IntelliJ + Maven

cuxqih21  于 2023-06-28  发布在  Java
关注(0)|答案(3)|浏览(119)

我试图连接我的java应用程序到我的sqlite数据库,但我得到了这个好的老错误。我正在使用Maven,我的设置看起来像这样:
project structure
我正在使用IntelliJ,据我所知,Maven集成得很好。我想知道我是否需要将库添加到其他地方,或者将jar文件放在类路径上。
我还在这里留下了包含项目定义的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.16.1</version>
        </dependency>
    </dependencies>

</project>

这是我的连接类:

package database;

import java.io.File;
import java.io.FileNotFoundException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by luiscosta on 3/29/17.
 */
public class DBConnection {

    private Connection conn;
    private String dbPath;

    /**
     * Constructor
     */
    public DBConnection(String name, String folder) throws FileNotFoundException {

        File dbDir = new File(folder);

        if (dbDir.isDirectory() && dbDir.exists()) { //confirms the database directory exists
            if (folder.charAt(folder.length() - 1) == '/') { //checks if the directory ends in /
                this.dbPath = folder + name;

            } else {
                this.dbPath = folder + '/' + name; //if not adds it in the end of the folder pah
            }

            //Connection

            try {
                Class.forName("org.sqlite.JDBC");

                System.out.println("db Path" + this.dbPath);
                this.conn = DriverManager.getConnection("jdbc:sqlite:ChunkMetaData");

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new FileNotFoundException("Directory not found!");
        }
    }

}
jogvjijk

jogvjijk1#

尝试使用3.15.1。
我也有同样的问题。我尝试了eclipse + maven / command line,但是ClassNotFoundException在3.16.1中被抛出。3.15.1没有问题

58wvjzkj

58wvjzkj2#

您必须将sqlite-jdbc-3.16.1.jar添加到类路径中。在Intellij中,转到您可以添加的模块设置。您可以从以下网址下载:https://bitbucket.org/xerial/sqlite-jdbc/downloads/
我尝试了下面的代码,运行良好。

public class SqliteConnectionTry {

   public static void main(String[] args) {
      connect();
  }

   public static void connect() {
      Connection conn = null;
      try {
          // db parameters
           String url = "jdbc:sqlite::memory";
          // create a connection to the database
           conn = DriverManager.getConnection(url);

           System.out.println("Connected!!");

         } catch (SQLException e) {
            System.out.println(e.getMessage());
         } finally {
            try {
               if (conn != null) {
                  conn.close();
               }
         } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

}

k7fdbhmy

k7fdbhmy3#

我只是遇到了同样的问题(顺便说一句,不同的驱动程序也会发生):
因此,问题是Maven下载了JAR,但它没有放入类路径中
为了避免手工操作,我将应用程序打包到一个jar文件中,其中包括驱动程序
我用plugin做了这个:

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>path/to/MainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

相关问题