简体   繁体   中英

How to reference correct JDBC Driver from JAR file?

I am using IntelliJ IDEA as my Java IDE.

My Java project is using a SQL database and from within IntelliJ, the program runs perfectly.

However, when compiling to a JAR file, I consistently receive the infamous No suitable driver found for jdbc:sqlite error.

I do have the sqlite-jdbc-3.15.1.jar library imported into my project for use. No matter what I do, I cannot get the JAR to run outside of the IDE.

I have tried placing the sqlite-jdbc-3.15.1.jar file within the "/lib" directory of my JAR project (as all my other libraries are).

Here is my Connection statement where the error is thrown:

conn = DriverManager.getConnection("jdbc:sqlite:" + QUICKNOTES_USER_DB_FILENAME);

I have found several other solutions to similar problems, but they all seem to be related to errors while developing the program. My problem is after deployment.

Short Answer

Add the JDBC lib to the CLASSPATH.

Longer Answer

In java there are at least two classpaths that matter; the compile time classpath and the runtime classpath. You appear to be having a problem with the runtime classpath.

When you to a jar, none of the project dependencies are included in the artifact (the jar file). You still need the dependencies when attempting to run the jar, so java looks for them on the runtime classpath.

Make sure that the desired JDBC lib is listed in the runtime classpath. Often, this means that you need to edit the CLASSPATH environment variable (either in the login environment or in a script file) to include the desired jar file (or files).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM