How to pack a SQLite database into a jar?

懵懂的女人 提交于 2020-01-03 04:23:07

问题


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?


回答1:


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.




回答2:


To solve your ClassNotFoundException, you'll need to add the sqllite jar to the classpath

see: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html




回答3:


My preference is to use a utility called one jar. You can use call it from ant or maven and I use it both for work and my personal projects. I have a decent blog article that explains how to use it: Building Self Contained Executable Jars 2 ways



来源:https://stackoverflow.com/questions/8358756/how-to-pack-a-sqlite-database-into-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!