0
votes

My first question on stackoverflow:

I'm on MacOS BigSur 11.5.2, 2.3 GHz Dual-Core i5, 8GB. I'm using Eclipse IDE for Java, v4.17. I've downloaded and configured the JavaFX SDK (v11.0.2) from gluonhq.com

I'm attempting to run a basic JavaFX HelloWorld program (very common, seen it everywhere). When I run this program, all it does is open a blue Java folder in my desktop's task bar; it doesn't open a new UI window titled "Hello World".

  • I've added the JavaFX SDK external library to the project's build path.

  • I've specified the correct path to the JavafX SDK in the Run Config's vm arguments.

    --module-path /Documents/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml

  • I've ensured javafx.controls and javafx.fxml module dependencies are recognized in the Run Config (I assume all necessary requires transitive modules are encompassed here).


here is my Main.java class

    package com.me.jfxapplication;
    
    import javafx.application.Application;
    import javafx.scene.Parent;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;


public class Main extends Application {
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = javafx.fxml.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);
    }
}

here is my Sample.fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.GridPane?>

<GridPane fx:controller="com.cowsill.jfxapplication.Controller"
    xmlns:fx="javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>

here is my module-info.java

    module com.me.jfxapplication {
    exports com.me.jfxapplication;

    requires javafx.base;
    requires javafx.fxml;
    requires javafx.graphics;
}

I've also attempted to run this on the command line via the following:

javac --module-path ~/Documents/javafx-sdk-11.0.2/lib/ --add-modules javafx.controls,javafx.fxml jfxapplication/Main.java

 java --module-path ~/Documents/javafx-sdk-11.0.2/lib/ --add-modules javafx.controls,javafx.fxml jfxapplication/Main.java

After scouring web resources, there seem to a lot of repeat issues regarding module dependencies with JFX. I went through every instance of the problem that others were having and and mirrored every bug fix they suggested. My program is running yes? it's just not displaying the scene graph/UI.

Thanks!

1
Your commands seem strange. (1) Your source directory structure should typically mirror the package structure, so I would expect the Main class to be located at com/me/jfxapplication/Main.java. Yet you seem to indicate it's located at jfxapplication/Main.java. (2) You don't appear to be compiling the module-info.java file. (3) As your application appears to have a module-info.java file you shouldn't need to use --add-modules. And you should be launching your application as a module via the --module <main-module>[/<main-class>] option.Slaw
(4) Even if your code was not modular your java command is still wrong. You should be passing the fully qualified class name, not the source file, at the end. So com.me.jfxapplication.Main, not jfxapplication/Main.java. I can't promise these issues are related to your problem though.Slaw
Are you following the steps here -> openjfx.io/openjfx-docs?Sedrick
Thank you @slaw, (1 & 4) I'm actually compiling and executing from inside the source directory, hence the abbreviated class path (and I did try the fully qualified class name during both compilation and execution, to no avail). (2) I wasn't aware that module-info.java need compiling in conjunction the Main; at what point would I compile it? (3) I tried compiling without declaring --add-modules for those jar's, but it returns 'error: package javafx.scene does not exist' and '...javafx.stage does not exist'.Therius_Traxdominus
Thank you @Sedrick, I have been following the openjfx.io guide verbatim with the exception of export PATH_TO_FX=path/to/javafx-sdk-15.0.1/lib... because when entering this command, it returns 'export: PATH_TO_FX=path/to/javafx-sdk-15.0.1/lib: not a valid identifier'. I've ensured my path_to_fx is accurateTherius_Traxdominus

1 Answers

0
votes

When using Eclipse IDE, click on Run -> Run Configurations... -> Java Application -> your project -> Arguments.

Make sure the checkbox "Use the -XstartOnFirstThread argument when launching with SWT" is not checked.

This should solve the problem.