简体   繁体   中英

Derby Database Communication Fails in Built .jar File

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM