0

I have created an java application which uses the following code to connect to a Derby Embedded Database:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby:myDatabase;create=true");

Everything works fine when using the Netbeans IDE and just running the application. But when I built the project and try to run the application via the .jar file the application starts but can not access the Derby Database.

I got the following StackTrace:

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Unknown Source)

But when I am building the project I included the following debendency to my pom.xml:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.14.1.0</version>
  <type>jar</type>
</dependency>
Zharin
  • 75
  • 12
  • Please provide more information, include the exception stacktrace. And, please, re-read your own question and ask yourself if you could answer a question with the little information provided. – Mark Rotteveel Jan 08 '18 at 10:41
  • I have now provided more information. I hope you can help me now. – Zharin Jan 08 '18 at 12:39
  • 1
    Your classpath doesn't include the derby driver when you run your application. Fix your classpath. See also https://stackoverflow.com/questions/17408769/how-do-i-resolve-classnotfoundexception – Mark Rotteveel Jan 08 '18 at 12:51
  • What do you mean with setting the classpath? – Zharin Jan 08 '18 at 12:58
  • The derby driver is not part of your application jar, you need to specify the required libraries on the classpath either on the command line or as part of the JAR manifest. – Mark Rotteveel Jan 08 '18 at 13:00

1 Answers1

0

I solved the problem by adding the following code to the build of my pom.xml file:

<build>
  <plugins>
    <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>MyMainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
  </plugins>
</build>
Zharin
  • 75
  • 12