0

I'm writing an app using JavaFX and H2 database using Intellij Idea 2016.3.5. I let Intellij creating a default JavaFX app and added 6 lines of my own code:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        // my code below
        try {
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("Class Not Found for h2 Driver");
            System.err.println(e);
        }
    }


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

and then I added h2-1.3.176.jar into the Modules dependency, so the dependency looks like:

1.8 (java version "1.8.0_60")
<Module source>
h2-1.3.176

It builds and runs fine. Then, I built the artifact of a jar file and tried to run it outside of Intellij:

/usr/lib/jvm/jdk1.8.0_60/bin/java -cp /home/opt/h2/bin/h2-1.3.176.jar -jar Fubar/out/artifacts/JavaFXApp/JavaFXApp.jar 

It throws an ClassNotFoundException: org.h2.Driver. But it runs fine if I modify the command slightly:

/usr/lib/jvm/jdk1.8.0_60/bin/java -cp Fubar/out/production/Fubar:/home/opt/h2/bin/h2-1.3.176.jar sample.Main

What does the JavaFXApp.jar miss? How can I build a jar file and runs h2-1.3.176.jar in the classpath?

Guangliang
  • 228
  • 2
  • 10

0 Answers0