0

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.

rothloup
  • 1,220
  • 11
  • 29
  • 2
    From memory, using `-jar` affects the use `-cp` as a jars can (and normally should) have their own configured `Class-Path` entry in the `META-INF/MANIFEST.MF` file. Most IDEs are capable of configuring this. If you're using Maven, you may need to supply some additional configuration steps to have it generate the manifest and to include the dependencies – MadProgrammer Jan 21 '18 at 23:24
  • It seems correct, but on Linux quotes are needed: java -cp '.:sqlite-jdbc-3.21.0.1.jar' -jar myapplication.jar In some other releases double quotes. – Black.Jack Jan 21 '18 at 23:39
  • @Black.Jack: The quotes are only needed to prevent shell expansion of wildcards. In any case, I tried your suggestion and it didn't change the outcome. – rothloup Jan 22 '18 at 01:01
  • This question has been marked as a duplicate, and based on the [accepted answer](https://stackoverflow.com/questions/15930782/call-java-jar-myfile-jar-with-additional-classpath-option/15930980#15930980), I able to solve my problem. The key was to understand that -cp and -jar cannot be used at the same time. – rothloup Jan 22 '18 at 01:25
  • However, although the problem is solved, the explanation is still missing: Why is this dependency treated differently than other dependencies? Why do I have to carefully add this library to my classpath, while other libraries are easily included automatically? – rothloup Jan 22 '18 at 01:26

0 Answers0