0

I am trying to learn OSGi and i made one simple project where it will just create a table in a Sqlite DB. I added the "sqlite-jdbc-3.7.2.jar" in the build path. But when i run that project, it says "ClassNotFoundException".

I use Eclipse IDE with BndTools.

I searched other posts and some people said to add the jar as a different bundle and then export it. I tried it. In workspace i selected Import->Plugins and Fragments -> Select The Directory. But it doesnot list the Jar (Works for other Jars but doesnot detect sqlite-jdbc-3.7.2.jar).

I have done like below,

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;
   }

 }

When i Run this i get the following error:

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! 

Please help me on this :)

Rajkishan Swami
  • 3,569
  • 10
  • 48
  • 68
  • Anyone can help me on this, please? – Rajkishan Swami Mar 25 '15 at 04:25
  • Have you found the solution or still looking – Raj Dec 11 '15 at 10:49
  • @Rajkishan did you guys find a way to solve this ? – Ashok May 04 '16 at 17:31
  • @Ashok : We are using BndTools Now. You can add the sqllite jar in the lib folder and add it to buildpath in `bnd.bnd` manually. Something like `-buildpath: \ osgi.core,\ osgi.cmpn,\ biz.aQute.bnd.annotation,\ lib/sqllitexxx.jar;version=file`. It should work. Sorry i don't know more on this as a senior guy built a Framework for us and we use it now. – Rajkishan Swami May 05 '16 at 13:22
  • @Rajkishan I tried same thing, Its not working do you think I need to use separate Jar file which can support OSGi !? – Ashok May 25 '16 at 05:01
  • @Ashok: You can try use the `sqllite` jar version `3.8.11.2`. As a reply here mentioned, this version comes packaged as a `Bundle`. Give it a try, it may work. – Rajkishan Swami May 25 '16 at 05:37
  • @Rajkishan Tried with sqlite-jdbc-3.8.11.2.jar still same error – Ashok May 25 '16 at 10:38
  • @Ashok : Well then... if you have not done already, you can try repackaging the jar as the bundle. http://stackoverflow.com/questions/29494485/convert-existing-jar-to-osgi-bundle – Rajkishan Swami May 25 '16 at 12:42
  • @Ashok : Wish i could help you with this, but i don't have access to the code written by our seniors :(. We just use the bundle as an api and thats it. – Rajkishan Swami May 25 '16 at 12:44

2 Answers2

1

Please take a look at a great OSGi tutorial here, it describes database connectivity and OSGi very well.

Best regards

Community
  • 1
  • 1
Tom
  • 181
  • 1
  • 8
0

The DriverManager uses the OSGI classloader (since the caller is loaded from the OSGI container). Because the org.sqlite.JDBC is not visible from the OSGI's classloader, you have a ClassNotFoundException.

The version 3.8.11.2 of SQLite JDBC is now an OSGI bundle so if you deploys the SQLite JDBC jar as an OSGI bundle rather than an embedded jar of your bundle, it should work.

Z0RrO
  • 83
  • 4
  • 11
  • I tried with adding SQlite Bundle with the help of https://github.com/bndtools/bndtools/wiki/How-to-Wrap-Bundles but has no effect. Problem remains same – Ashok May 25 '16 at 12:24