编译成Jar后找不到Derby EmbeddedDriver类

时间:2017-08-24 18:46:51

标签: java jdbc intellij-idea jar derby

我正在使用Derby Embedded Driver创建可执行jar。从IDE启动时,应用程序按预期运行(我使用IntelliJ)。创建jar后会收到错误,因此我认为问题在于依赖项未编译到jar中。

但是,derby.jar文件显示在外部库下。我还尝试手动添加... \ java \ jdk1.8.0_131 \ db \ lib作为依赖项,但没有用。

编辑:该项目是一个JavaFX应用程序,由IntelliJ预设的JavaFX应用程序项目类型生成。添加db \ lib依赖项后,我设置了项目结构>项目设置>用于生成JAR的工件(我还使用了生成Java FX应用程序而没有结果差异。)我还添加了META-INF / MANIFEST.MF。

我的问题是,我做错了什么,或者错过了什么。

代码:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.derby.jdbc.EmbeddedDriver;

import java.sql.Connection;
import java.sql.DriverManager;

public class Main extends Application {

    private final String connectionURL = "jdbc:derby:rbdb;create=true";
    private Connection connection;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        DriverManager.registerDriver(new EmbeddedDriver());
        System.out.println("Driver loaded");
        connection = DriverManager.getConnection(connectionURL);
        if (connection != null) {
            System.out.println("Connected to DB");
        }

    //Default JavaFX stuff auto-generated by IntelliJ
    //Controller.java and sample.fxml are defaults as well
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}
}

错误:

C:\Users\<Username>\IdeaProjects\HelloWorld\out\artifacts\HelloWorld_jar>java -jar HelloWorld.jar
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherIml.java:389)
    at sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
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)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at sample.Main.start(Main.java:20)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application sample.Main

META-INF / MANIFEST.MF

Manifest-Version: 1.0
Main-Class: sample.Main

以下是其他类似问题的列表,但是,这些问题都没有帮助:

  1. 有关驱动程序的其他Stackoverflow问题:
  2. 关于建造一个罐子的其他问题
  3. 我还从tutorialspoint查看了嵌入式JDBC应用程序的教程:

0 个答案:

没有答案