I have seen following topics, but they doesn't post solution to my question:
- java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
- JDBC Derby driver not found
- SQLException: No suitable driver found for jdbc:derby://localhost:1527
- Class [org.apache.derby.jdbc.ClientDriver] not found Exception
- Class [org.apache.derby.jdbc.ClientDriver] not found. When trying to connect to db
- 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:
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.
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.
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