Reputation: 75
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>
Upvotes: 0
Views: 283
Reputation: 75
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>
Upvotes: 0