简体   繁体   English

内置.jar文件中的Derby数据库通信失败

[英]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: 我创建了一个Java应用程序,该应用程序使用以下代码连接到Derby嵌入式数据库:

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. 使用Netbeans IDE并仅运行应用程序时,一切工作正常。 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. 但是,当我构建项目并尝试通过.jar文件运行该应用程序时,该应用程序将启动,但无法访问Derby数据库。

I got the following StackTrace: 我得到以下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: 但是,当我构建项目时,我的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: 我通过将以下代码添加到pom.xml文件的构建中解决了该问题:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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