Search code examples
javasqlitepackaging

How to pack a SQLite database into a jar?


I've got a Java project, which uses a small SQLite database.

Now I want to create an executable jar File, with the Database file and the driver (sqlitejdbc-v056) inside to have a single, all containing package.

My package structure looks like this:

Bank
|
| 
+---src
|   ...
|           
+---bin
|   ...
|           
+---data
|       bank_database.db
|       
+---img
|       ajax-refresh-icon.gif
|       
+---doc
|       Datenbankschema.uxf
|       
\---resources
        sqlitejdbc-v056.jar

I access the DB with this small Java class:

package model;

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

public class DBHandle {

    Connection conn;

    public DBHandle() throws Exception {
        Class.forName("org.sqlite.JDBC");
    }

    public Connection openConnection() throws SQLException {
        conn = DriverManager.getConnection("jdbc:sqlite:data\\bank_database.db");
        return conn;
    }

    public void closeConnection() throws SQLException {
        if (!conn.isClosed()) {
            conn.close();
        }

    }

}

If I pack all this stuff into a jar with the Eclipse export manager and try to execute it it throws a ClassNotFoundException for the JDBC driver.

How can I fix this problem?

Is it even possible to modify resources into a jar regarding to the database?


Solution

  • AFAIK you can't modify resources inside a jar without much hassle (basically it is a zip and thus it should be possible, but there might be file locks by the JVM etc.).

    Additionally, you can't put one jar into another. However, you could unpackage the driver jar and include the contents in your jar, if you want that. The Maven assembly plugin has a goal that does this: jar-with-dependencies.