outellou outellou - 1 year ago 651
Java Question

How to include SQLite database in executable Jar?

I have created a Swing application that uses SQLite as a local database. The database file is located in project's root directory.


The application runs fine on Eclipse, but when I run the packaged executable Jar, I get the following error:

No such table : table1

This means that the database is not reachable. When I examined the contents of the resulting JAR file, the database file was not there anymore.

In the code, I've linked the database as follows:


My question is, how to include the SQLite database in the executable Jar?


When I placed the DB file in the source Folder
and changed the path to
, it worked on Eclipse but again when running the Jar file as
java -jar Project.jar
. It said:

path to 'src/DatabaseFile': 'C:\Users\name\src' does not exist

I think I need to specify a relative path for the database.


This is how I connect to the database:

public Connection getConnection(){
con = DriverManager.getConnection("jdbc:sqlite:src/DatabaseFile");

} catch (Exception e) {
Log.fatal("Méthode: getConnection() | Class : SQLiteConnection | msg system : " + e.getMessage());
return con;

dsh dsh
Answer Source

What library are you using for SQLite?

I did a search based on the connection URI you indicated and found this one. In the documentation it says:

2009 May 19th: sqlite-jdbc- released. This version supports "jdbc:sqlite::resource:" syntax to access read-only DB files contained in JAR archives, or external resources specified via URL, local files address etc. (see also the detailes)

If that is the driver you are using, then I would suggest the following connection URI:


The key is that since your database is in a jar file, it can not be access as a file with FileInputStream. Instead it must be accessed through the JVM's support for it (namely with Class.getResource() or Class.getResourceAsStream()). Do note that resources contained within jar files are read-only. You won't be able to save any changes to your database.