0

I first time try to use JavaFx with Maven. By this topic: link IntelliJ can't recognize JavaFX 11 with OpenJDK 11, I configured project. But whatever I do I can't load fxml file, because "getClass().getResource(path)" return null.

I changed path, start it with '/' and without, changed packages, created packages, deleted packages, changed references in module-info, but this is not work.

struct: https://ibb.co/Hhwzk8b

module LogAggregator {
    requires javafx.fxml;
    requires javafx.controls;

    opens fxml to javafx.fxml;
    exports com.github.PavelKisliuk;
}

//----------------------------------------------------

public class Main extends Application {
    public void start(Stage primaryStage) throws Exception {

              String path = "fxml/Input.fxml";
              FXMLLoader fxmlLoader = new FXMLLoader();
      fxmlLoader.setLocation(getClass().getResource(path));
      Parent fxmlMainWindow = fxmlLoader.load();

      //start-up window
      //-----------------------------------------------
      Scene s = new Scene(fxmlMainWindow);
      primaryStage.setScene(s);
      primaryStage.show();
  }

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

May be somebody know this problem and can help me. Without maven I don't have any problem's.

DECISION

Such path:

String path = "/fxml/Input.fxml";

Plus two string's to module-info:

opens com.github.PavelKisliuk.controller to javafx.fxml;
exports com.github.PavelKisliuk.controller to javafx.fxml;
NotZack
  • 518
  • 2
  • 9
  • 22
  • 1
    You're currently using a relative path, which is being resolved against `Main`. You need to use an absolute path here: `"/fxml/Input.fxml"`. See [the documentation](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)) of `getResource` for more information. – Slaw Sep 01 '19 at 15:51
  • Check, if the directory IntellIJ uses as classpath to run the program include a subdirectory `fxml` that contains the `Input.fxml` file and use the path `"/fxml/Input.fxml"`... – fabian Sep 01 '19 at 16:57

1 Answers1

0

Your Main class seems to be in the package com.gihub.PavelKisliuk, but resources are under fxml. In this situation relative path you use resolves to non-existent com.gihub.PavelKisliuk/fxml/Input.fxml.

Solutions:

  1. Preferrable: move resources into 'com.gihub.PavelKisliuk'
  2. Move Main class out of 'com.gihub.PavelKisliuk'

Hope this helps...

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60