【问题标题】:ERROR [stderr] (EJB default - 8) java.lang.ClassNotFoundException: org.sqlite.JDBC错误 [stderr] (EJB 默认 - 8) java.lang.ClassNotFoundException: org.sqlite.JDBC
【发布时间】:2016-03-08 09:46:33
【问题描述】:

我正在尝试与 j2ee 项目中的 sqlite 数据库建立连接,但我不断收到此错误“错误 [stderr] (EJB 默认 -8)java.lang.ClassNotFoundException: org.sqlite.JDBC”。知道我在 java 程序中测试了该方法并且工作正常。 我在 ejb 项目(会话 bean)中有一个连接类,这里有代码。

public Connection getConnection(){
    Connection connection= null;


    try {
        Class.forName("org.sqlite.JDBC");
        String dataFolder = System.getProperty("user.home") + "\\AppData\\Local";
        String b = System.getProperty("user.home") ;



        String file = b + "\\Desktop\\file";
        connection = DriverManager.getConnection("jdbc:sqlite:"+ file );
       System.out.println("Connection completed.");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return connection;
 }

然后我在 java 项目中使用 junit 测试对其进行测试,但每次我得到相同的错误:java.Lang.ClassNotFoundException: org.sqlite.JDBC PS:我有这样的sqlite.jar(右键单击项目->配置构建路径->库->添加外部jar)但总是面临同样的错误

【问题讨论】:

  • java.Lang.ClassNotFoundException 真的发生在什么时候?!无论是在您的应用程序运行时还是在您运行 JUnit 测试时?
  • 当我运行 junit 测试时@aribeiro
  • 您是否使用 Maven 来构建和打包您的应用程序?
  • 我使用 jboss 7.1 作为服务器,使用 eclipse 作为 IDE @aribeiro

标签: java sqlite jakarta-ee ejb database-connection


【解决方案1】:

我已经解决了这个问题:

  1. 在此路径中添加了 SQLite jar 文件:

    D:\jboss-as-7.1.1.Final\modules\javax\activation\api\main

  2. JBoss Server Runtime > Deployments > Manage Deployments 添加了 SQLite jar 文件,然后启用它。

【讨论】:

  • Jboss 服务器运行时在哪里 > 部署 > 在 Eclipse 中管理部署?
【解决方案2】:

假设你的项目结构如下:

src/main/java
     |
      - MyConnection.java
src/test/java
     |
      - MyConnectionTest.java

具有以下元素:

基类MyConnection

public class MyConnection {

    public Connection getConnection() {
        Connection connection = null;

        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:");
            System.out.println("Connection completed.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return connection;
    }
}

测试类MyConnectionTest

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

public class MyConnectionTest {

    @Test
    public void isValid() throws SQLException {
        MyConnection myConn = new MyConnection();
        Connection conn = myConn.getConnection();
        assertTrue(conn.isValid(0));
        conn.close();
        assertFalse(conn.isValid(0));
    }
}

在您的 Eclipse 项目中,您只需执行以下操作:

  1. 右键单击您的项目;
  2. 导航到构建路径并选择选项配置构建路径...;
  3. 在右侧菜单中,按添加库,选择JUnit (JUnit4) 并单击完成
  4. 最后,在 Configure Build Path... 菜单上,选择选项 Add External JARs...,浏览到您的 SQLite JDBC jar 文件所在的位置,选择它并点击应用
  5. 转到您的 MyConnectionTest 测试类并将其作为 JUnit 测试运行。

【讨论】:

  • 我得到同样的错误:错误 [stderr] (EJB 默认 - 10) java.lang.ClassNotFoundException: org.sqlite.JDBC from [Module "deployment.mrhelp-server-ejb.jar:main "来自服务模块加载器]。我的项目结构是这样的:mrhelp-server-ejb -> ejbmoule->connection package->connection.java,connectionRemote mrhelp-client->conection delegate package->connection delagate.java ->connectionTest package->test connection.java
  • 连接延迟在这里:private static String jndi="ejb:/mrhelp-server-ejb/ManageService!tn.esprit.tex.gestion.service.connectionRemote"; static connectionRemote getProxy(){ return (connectionRemote) ServiceLocator.getInstance().getProxy(jndi); } 公共静态连接 getConnection() { return getProxy().getConnection(); }
  • @user2096996,您刚刚发布的错误是指 mrhelp-server-ejb.jar 文件。您是如何执行测试的?
  • 有测试连接的代码@Test public void dotest() throws SQLException { ConnectionDelegate.getConnection();连接 myConn = 新连接();连接 conn = myConn.getConnection(); assertTrue(conn.isValid(0)); conn.close();断言假(conn.isValid(0)); }
  • @user2096996,您能否使用上次 cmets 中提到的信息更新您的问题?并且,如果可能的话,请格式化它。
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2013-07-23
  • 2016-03-14
  • 2021-08-28
相关资源
最近更新 更多