8

I have seen following topics, but they doesn't post solution to my question:

  1. java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
  2. JDBC Derby driver not found
  3. SQLException: No suitable driver found for jdbc:derby://localhost:1527
  4. Class [org.apache.derby.jdbc.ClientDriver] not found Exception
  5. Class [org.apache.derby.jdbc.ClientDriver] not found. When trying to connect to db
  6. ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver when trying to use JPA with Derby

Hello.
In my project I am using Maven and I also wanted to use Derby Database in Embedded Mode. Therefore I updated pom.xml file by the following:

<dependencies>
    ...
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.12.1.1</version>
    </dependency>
</dependencies>

Maven succesfully downloaded the dependency. It was visible in the Eclipse's BuildPath under Maven Dependencies as derby-10.12.1.1.jar.
Then I created test class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.derby.jdbc.EmbeddedDriver;

public class DerbyTest {
    public static void main (String...strings){
         try {
             DriverManager.registerDriver(new EmbeddedDriver());
            Connection conn = DriverManager.getConnection("jdbc:derby:test2;create=true");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

which gave me following ClassNotFoundException

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/derby/jdbc/EmbeddedDriver
    at DerbyTest.main(DerbyTest.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

The question is - why? The class is in the build path. Eclipse didn't gave me any compile-time error warning - it sees the class during writing. Dependency's scope is compile. JVM shouldn't have problems with finding the class. Also JVM doesn't have problem with other dependencies I am using (jTest, Hibernate, SQLite).

Post Scriptum: I tried to solve this puzzle:

  1. I created new project with the same test class as above. Then I manually downloaded Derby from https://db.apache.org/derby/releases/release-10.12.1.1.cgi (bin version) and manually added derby.jar to the new project's build path. After running a program the database was sucesfully created.

  2. Then I created another new project 2 with the same test class as previously. I've found Maven's version of Debry in my local repository (in C:\Users\User\.m2\repository\org\apache\derby\derby\10.12.1.1) and manually added derby-10.12.1.1.jar to the new project's build path. After running a program I got exactly the same error as using Maven.

  3. I used manually downloaded Derby (from point 1.) and created external repository by editing pom.xml file in my main project:

.

<repositories>
    <repository>
        <id>derby-repo</id>
        <url>file://C:/libs</url>
    </repository>
</repositories>

<dependencies>
    ...
    <dependency>
        <groupId>derbygroupid</groupId>
        <artifactId>derby</artifactId>
        <version>0.0.1</version>
    </dependency>
</dependencies>

Maven succesfully added an dependency in the Eclipse's BuildPath under Maven Dependencies as derby-0.0.1.jar. After running a program the database was sucesfully created.
This generally solves the problem, but the question is, why Maven couldn't handle it on it's own?

EDIT: For those who are interested. I've temporary solved problem by using older version of derby:

<dependencies>
    ...
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.11.1.1</version>
    </dependency>
</dependencies> 

This version works, but I still have no idea why 10.12.1.1 didn't

Community
  • 1
  • 1
Tyvrel
  • 198
  • 2
  • 10
  • Perhaps the clue is in the term `BuildPath`. Perhaps the `BuildPath` is only used when you **build** your project, not when you **run** your project? – Bryan Pendleton Sep 01 '16 at 16:03
  • How to determine whether BuildPath is used only for building? On the other hand in **the same project** manually downloaded derby works while maven's derby doesn't. – Tyvrel Sep 01 '16 at 17:16
  • 1
    2021 and still facing the same issue, thanks for the answer. – ADSquared Mar 22 '21 at 14:58

3 Answers3

0

I was facing the same issue, but I got past it...

Since I was working on intellij, I went to

File->Project Structure->Modules->Dependencies

Clicked on the plus sign and selected JAR's and Dependiencies

Opened the path to jdk, and from within it, added the following jar files 1. derby.jar 2. derbyclient.jar 3. derbynet.jar 4. derbytools.jar 5. derby.jar

Clicked on Apply. It worked!

For Eclipse, try this.

0

Although I still haven't understood yet why this is the case: Since Java 1.6 your call to DriverManager.getConnection() will succeed even if you do not register the Driver manually. Try to omit creating an instance of EmbeddedDriver to register the driver and just get the Connection object. I got the same Exception when calling Class.forName("org.apache.derby.jdbc.EmbeddedDriver") and simply removed it.

However, a shutdown of Derby will deregister the driver. So if you intend to reboot Derby, this trick will only work once until you shutdown Derby. Passing a "deregister=false" on shutdown will make sure the Derby Driver is not removed after the initial register.

I am using 10.15.2.0 of the same Maven package and do not have further derby packages in my pom.xml.

toaster
  • 61
  • 7
0

I was facing the same issue and by reverting to the 10.11.1.1 version of the driver everything worked.

Anthony
  • 135
  • 1
  • 14