Simple Java 11 JavaFXprogram, Error when run from JAR file (Windows)

NottmTony :

I have a test JavaFX program that runs fine in Eclipse. - I had to add a module-info file (module-info.java) containing the following:

module moduletest {
    requires javafx.controls;
    exports com.javafx.test.MenuTest;
}

Java class:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class MenuTest extends Application {

@Override
public void start(Stage primaryStage) {

    Scene scene = new Scene(new VBox(), 300, 250);

    Circle circle1 = new Circle(-20,0,200);        
    circle1.setFill(Color.CADETBLUE);
    ((VBox) scene.getRoot()).getChildren().addAll(circle1);

    primaryStage.setScene(scene);
    primaryStage.show();        
}

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

When I run it as an exported JAR file using the following command

java -jar TonyJavaFX.jar -classpath .;./lib/javafx.controls.jar

, I get the following error:

Error: Could not find or load main class com.javafx.test.MenuTest.MenuTest Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

Note: I have a lib folder directly under the folder I am running the jar from, which contains all the JavaFX jar files.

Naman :

You need to make sure that the modules javax.controls and javafx-graphics.jar are present on your modulepath instead of the classpath.

--module-path ./lib/javafx.controls.jar:./lib/javafx.graphics.jar

Note: This wouldn't require an addition of javafx.graphics module in your module declaration, since it is transitively required by the javafx.controls module. Just the presence of the module in the modulepath shall help it.

Important: You should ideally be adding the complete lib folder to modulepath. More here - https://openjfx.io/openjfx-docs/#install-javafx

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=102477&siteId=1