【问题标题】:Using SqLite JDBC Jar in a OSGi Project在 OSGi 项目中使用 Sqlite JDBC Jar
【发布时间】:2015-05-27 20:29:23
【问题描述】:

我正在尝试学习 OSGi,我做了一个简单的项目,它将在 Sqlite DB 中创建一个表。我在构建路径中添加了“sqlite-jdbc-3.7.2.jar”。但是当我运行该项目时,它会显示“ClassNotFoundException”。

我使用带有 BndTools 的 Eclipse IDE。

我搜索了其他帖子,有人说将 jar 添加为不同的捆绑包,然后将其导出。我尝试过这个。在工作区中,我选择了 Import->Plugins and Fragments -> Select The Directory。但它没有列出 Jar(适用于其他 Jar 但不检测 sqlite-jdbc-3.7.2.jar)。

我已经完成了如下操作,

se.sample.sqlitedbconn.ConnActivator.java

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.Statement;
  import org.osgi.framework.BundleActivator;
  import org.osgi.framework.BundleContext;
  import se.sample.connproperties.CreateTable;
  import aQute.bnd.annotation.component.*;

 @Component
 public class ConnActivator implements BundleActivator {

Connection connection;
Statement statement;

@Activate
public void start(BundleContext context) throws Exception {
    System.out.println("Starting");


    try {
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite://home/raj/Desktop/EclipseProjectsWorkspace/ZWave.db");
    } catch (Exception ex) {
        System.err.println(ex.getClass().getName()+": "+ex.getMessage());
    }

    System.out.println("Connection To Database Successful");

    CreateTable createTable = new CreateTable();
    statement = connection.createStatement();
    boolean isTableCreated = createTable.createANewTable(connection,statement);

        if(isTableCreated){
             System.out.println("Table Created Successfully!");
        }else{
             System.out.println("Table Creation Failed!");
        }

        statement.close();
    }

     @Deactivate
     public void stop(BundleContext context) throws Exception {
         System.out.println("Stopping");

        if(connection != null){
            connection.close();
        }

         System.out.println("Database Connection Closed!");
    }

 }

se.sample.connproperties.CreateTable.java

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

public class CreateTable {


     public boolean createANewTable(Connection connection, Statement statement) {
    String sql =    "CREATE TABLE Product " +
                    "(ProductId INT PRIMARY KEY  NOT NULL,"+
                    "ProductName TEXT NOT NULL,"+
                    "ProductPrice REAL);";
    try {
        statement.executeUpdate(sql);
    } catch (SQLException e) {
        System.err.println(e.getClass().getName()+": "+e.getMessage());
        return false;
    }
    return true;
   }

 }

当我运行这个时,我得到以下错误:

Starting
Connection To Database Successful
java.lang.ClassNotFoundException: org.sqlite.JDBC not found by se.sample.sqlitedbconn [8]
! Failed to start bundle se.sample.sqlitedbconn-0.0.0.201503241305, exception activator error null from: se.sample.sqlitedbconn.ConnActivator:start#34
 ____________________________
 Welcome to Apache Felix Gogo

 g! 

请帮帮我:)

【问题讨论】:

  • 有人可以帮我吗?
  • 您找到了解决方案还是还在寻找
  • @Rajkishan 你们找到解决这个问题的方法了吗?
  • @Ashok:我们现在正在使用 BndTools。您可以在 lib 文件夹中添加 sqllite jar 并手动将其添加到 bnd.bnd 中的 buildpath。像-buildpath: \ osgi.core,\ osgi.cmpn,\ biz.aQute.bnd.annotation,\ lib/sqllitexxx.jar;version=file 这样的东西。它应该工作。抱歉,作为一名资深人士,我对此了解不多,为我们构建了一个框架,我们现在就使用它。
  • @Rajkishan 我试过同样的事情,它不工作你认为我需要使用可以支持 OSGi 的单独 Jar 文件!?

标签: java jdbc osgi bndtools


【解决方案1】:

请看一个很棒的 OSGi 教程here,它很好地描述了数据库连接和 OSGi。

最好的问候

【讨论】:

    【解决方案2】:

    DriverManager 使用 OSGI 类加载器(因为调用者是从 OSGI 容器加载的)。因为 org.sqlite.JDBC 在 OSGI 的类加载器中不可见,所以您有一个 ClassNotFoundException。

    SQLite JDBC 3.8.11.2 版现在是一个 OSGI 包,因此如果您将 SQLite JDBC jar 部署为 OSGI 包而不是包的嵌入式 jar,它应该可以工作。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2017-08-17
    相关资源
    最近更新 更多