4

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.

NottmTony
  • 447
  • 1
  • 6
  • 28
  • 2
    In addition to the answer by _nullpointer_, you should be launching your code as a module; use `--module` (or the shorthand version, `-m`) instead of `-jar`. As far as I know, when using `-jar` it doesn't treat the JAR file as a module. This means the `module-info` file is ignored and the system doesn't know to resolve the `javafx.controls` or `javafx.graphics` modules. – Slaw Dec 11 '18 at 06:49

1 Answers1

3

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

Naman
  • 27,789
  • 26
  • 218
  • 353
  • I still get the same error on adding the graphics.jar. This is just a test program but yeah agreed I would use module path in "real life" - just trying to get something working. – NottmTony Dec 10 '18 at 15:56
  • @NottmTony How have you created the *.jar* that you're trying to execute? You can follow the documentation link and then follow as mentioned in *Runtime images* => *Modular CLI* – Naman Dec 10 '18 at 16:10
  • 1
    @NottmTony Its should ideally be a must to be added to *modulepath*. Reason - else it would result in one common unnamed module from where it might not be getting resolved. – Naman Dec 10 '18 at 16:15
  • 1
    Thanks this worked for me: javac --module-path "C:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls MenuTest.java – NottmTony Dec 11 '18 at 08:36
  • @NottmTony Cool. Do accept as an answer if this could help others as well. – Naman Dec 11 '18 at 08:44
  • 1
    I decided to build and run at command line and this worked - thanks BUILD javac --module-path "C:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls MenuTest RUN java --module-path "C:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls MenuTest – NottmTony Dec 11 '18 at 08:49