1

I'm creating an executable jar using the Derby Embedded Driver. The application runs as expected when launched from the IDE (I'm using IntelliJ). The error is received after a jar has been created, as such I believe that the problem lies in the dependency not being compiled into the jar.

However, the derby.jar file appears under the External Libraries. I've also tried to manually add the ...\java\jdk1.8.0_131\db\lib as a dependency, with no avail.


EDIT: This project is a JavaFX Application, generated from IntelliJ's preset JavaFX Application project type. After having added the db\lib dependency, I set up the Project Structure > Project Settings > Artifacts to generate a JAR (I have also used the generate Java FX Application with no difference in result.) I have also added the META-INF/MANIFEST.MF.


My question is, what have I done wrong, or missed.


Code:

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);
}
}

Error:

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

Here is a list of other similar issues, however, none of them have helped:

  1. Other Stackoverflow questions regarding the Driver:
  2. Other question regarding building a jar
  3. I've also looked at a tutorial for Embedded JDBC application from tutorialspoint:
Komodo2013
  • 11
  • 3
  • 1
    What type of project is this? How did you compile it? What is the content of `META-INF/MANIFEST.MF` in your Jar. Note that in normal ways of compiling Java, external dependencies are **not** included in the compiled jar, they are referenced from the manifest (which depending how your project is managed/compiled, you may need to do yourself). – Mark Rotteveel Aug 25 '17 at 09:00
  • @MarkRotteveel Answered your questions in the edit. I understand that it is not normal that I want the jars included, and indeed, it isn't necessary since the jar comes with the JRE anyway. With that being said, I still don't understand why it can't find the driver as, whether or not the jar is included in compilation, the JVM should still be able to find the required jar in the JRE. – Komodo2013 Aug 26 '17 at 02:13

0 Answers0