'"Class Not Found: JDBC driver org.apache.derby.jdbc.EmbeddedDriver could not be loaded" despite having included required JARs in classpath

While following this tutorial, in order to test if everything is set up correctly, I ran the 'setup' Ant target from Eclipse. The following is the error message I got:

    Buildfile: C:\Users\bingis_khan\eclipse-workspace\JDBCTutorial\build.xml
setup:
drop-tables:
     [echo] C:\Users\bingis_khan\eclipse-workspace\JDBCTutorial\classes;C:\Users\bingis_khan\Downloads\db-derby-10.15.2.0-bin\db-derby-10.15.2.0-bin\lib\derby.jar;C:\Users\bingis_khan\eclipse-workspace\JDBCTutorial\lib\JDBCTutorial.jar;C:\apache\xalan-j_2_7_2\xalan.jar

BUILD FAILED
C:\Users\bingis_khan\eclipse-workspace\JDBCTutorial\build.xml:141: The following error occurred while executing this line:
C:\Users\bingis_khan\eclipse-workspace\JDBCTutorial\build.xml:84: Class Not Found: JDBC driver org.apache.derby.jdbc.EmbeddedDriver could not be loaded

Total time: 374 milliseconds

This [echo] here is set up to print the CLASSPATH. As the next section shows, the path(s) in classpath match the actual locations of these archives:

Directory of C:\Users\bingis_khan\Downloads\db-derby-10.15.2.0-bin\db-derby-10.15.2.0-bin\lib

04/20/2021  08:57 PM    <DIR>          .
04/20/2021  08:57 PM    <DIR>          ..
04/20/2021  08:57 PM         3,683,035 derby.jar
04/20/2021  08:57 PM             1,490 derby.war
04/20/2021  08:57 PM           601,103 derbyclient.jar
04/20/2021  08:57 PM            83,942 derbyLocale_cs.jar
04/20/2021  08:57 PM            99,882 derbyLocale_de_DE.jar
*snip*

What I've tried:

  • Experimented with changing backslashes to slashes and vice-versa.
  • Tried changing derby.jar to derbyclient.jar.
  • Just in case imported the archive (outside of Ant) to the Eclipse project. None of the above methods worked (or even changed the output of "setup").

Where I've looked:

  • here - Poster did not add derby.jar to classpath.
  • also here - Different circumstances, but still applied the solution - nothing.
  • Some others, but most referred to runtime ClassNotFoundException. I haven't even gotten that far...

EDIT: Also attaching the relevant Ant target:

<target name="drop-tables">
    <echo>${toString:CLASSPATH}</echo>
    <sql driver="${DB.DRIVER}" url="${DB.URL}" userid="${DB.USER}"
         password="${DB.PASSWORD}" classpathref="CLASSPATH"
         delimiter="${DB.DELIMITER}" autocommit="false" onerror="continue">
      <transaction src="./sql/${DB.VENDOR}/drop-tables.sql"></transaction>
    </sql>
  </target>


Solution 1:[1]

Since you're using Derby 10.15, you have to also have derbyshared.jar in your classpath for the embedded driver.

More information here: http://db.apache.org/derby/releases/release-10.15.1.3.cgi#Release%20Notes%20for%20Apache%20Derby%2010.15.1.3

In particular, note this part:

A new jar file (derbyshared.jar) has been added. All Derby configurations require it. In addition, the derbytools.jar library is now required when running the network server, when using Derby DataSources, and when directly referencing the JDBC drivers.

Solution 2:[2]

verry important Since you're using Derby 10.15, you have to also have derbytools.jar in your classpath for the embedded driver. db-derby-10.15.2.0-bin\lib\derbytools.jar\org\apache\derby\jdbc\EmbeddedDriver.class Use 7zip to analyse!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bryan Pendleton
Solution 2 Wolfgang Fredrich