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.
- Image showing Derby.jar is in the External Libraries
- Image showing that lib contains ...\java\jdk1.8.0_131\db\lib and is set to be compiled
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:
- Other Stackoverflow questions regarding the Driver:
- Setting up an embedded Derby database in a standalone Java application
- Creating/Configuring Derby JDBC Client in IntelliJ Idea 13
- The infamous java.sql.SQLException: No suitable driver found
- SQLException: No suitable driver found for jdbc:derby://localhost:1527
- JDBC Derby driver not found
- java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?
- Other question regarding building a jar
- I've also looked at a tutorial for Embedded JDBC application from tutorialspoint: