I am having a lot of trouble using the JDBC library to work for me.
I am getting a "java.lang.ClassNotFoundException: org.sqlite.JDBC" error when my class tries to load the JDBC class at the Class.forName()
call.
public static void main(String[] args) {
try {
Class.forName("org.sqlite.JDBC"); //<--- Throws error
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
I followed the instructions given on the JDBC page, which states that I must ensure that the sqlite-jdbc JAR file is in the classpath. I did this by copying the sqlite-jdbc file into the same directory as my application's compiled JAR file, and then calling my applications using
java -cp .:sqlite-jdbc-3.21.0.1.jar -jar myapplication.jar
This seems like it should be quite straightforward, but it still gives the error indicating that it can't find the class. I've confirmed that the sqlite-jdbc-3.21.0.1.jar contains class "JDBC" in package "com.sqlite".
unzip -l sqlite-jdbc-3.21.0.1.jar | grep JDBC.class
2499 2017-12-07 12:10 org/sqlite/JDBC.class
The directory listing where my application runs looks like this:
myapplication.jar sqlite-jdbc-3.21.0.1.jar
It seems to me that since the current directory is in the default classpath, and I've copied the sqlite-jdbc-3.21.0.1.jar
into the same directory as my application's jar, then shouldn't java be able to find the class even if I don't set the classpath? And it seems even more strange that when I call out the sqlite-jdbc-3.21.0.1.jar
directly in the classpath that java still can't find the class. what gives?
Finally, I'd appreciate an explanation as why this dependency is different than other dependencies. I'm used to finding a library, adding a line in gradle to list it as a dependency using compile <dependency>
, and voila, it can find the path when compiling and when deploying. OTOH, I'm used to developing for Android (not a stand-alone application, as this is), where I'm guessing that all system libraries are always in the classpath and any non-Android dependencies are included in my application's JAR file. But why is the JDBC not included in my applications JAR, while other dependencies are?
I've read and implemented the answer to this question, so I don't believe that simply setting the classpath is the problem - unless I've somehow set it improperly.