-2

I have a small JavaFX app. Everything looked fine, and was working fine. Suddently, I wanted to convert this project to a Maven project (I wanted to add some external dependencies and use them in the app).

In Eclipse I have right-clicked the project, selected Configure and then Convert to Maven project.... Everything went fine, until I have build and run the application. The whole app is working perfectly, but there was a logo in the app window, and after converting the project to Maven - the image has dissapeared.

I am using JavaFX with FXML and SceneBuilder.

In the RootLayout.fxml file, there is an entry:

<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="225.0" layoutY="50.0" pickOnBounds="true" preserveRatio="true">
   <image>
      <Image url="@../../../../../../resources/images/nbtc.jpg" />
   </image>
</ImageView>

It is still visible in SceneBuilder after conversion to Maven, but it has disappeared from the app after running it.

Here is the folder structure:

enter image description here

Funny thing is, that the programicon32.png is working correctly and it is shown in the application. But it is defined in another place, outside the FXML file:

@Override
public void start(Stage primaryStage) {
    ...
    this.primaryStage.getIcons().add(new Image("file:resources/images/programicon32.png"));
    initRootLayout();
    ...
}

EDIT:

I have moved the image to the same package where RootLayout.fxml is, and have selected the moved image in SceneBuilder, the image is showing now, but still, this is not the droid that i'm looking for...

fabian
  • 80,457
  • 12
  • 86
  • 114
hc0re
  • 1,806
  • 2
  • 26
  • 61
  • Possible duplicate of [Using image resources in a jar using maven](https://stackoverflow.com/questions/9714028/using-image-resources-in-a-jar-using-maven) –  Feb 15 '19 at 14:53
  • [This answer](https://stackoverflow.com/a/53148244/6395627) may help. – Slaw Feb 15 '19 at 15:40

2 Answers2

2

The project folder structure is different to the structure of the classpath at runtime. Assuming the resources are included in the build path properly, the absolute paths of the image and fxml in the classpath at runtime are

/images/nbtc.jpg

and

/com/l4a/mbtc/application/util/RootLayout.fxml

Therefore the image needs to be specified as

<Image url="@../../../../../images/nbtc.jpg" />

You also shouldn't rely on the image being available as file at runtime. Usually the resources are included as entry in a .jar file; this does not allow access via the file: protocol. Use getResource instead:

this.primaryStage.getIcons().add(new Image(getClass().getResource("/images/programicon32.png").toExternalForm()));
fabian
  • 80,457
  • 12
  • 86
  • 114
1

By convention maven uses a specific directory structure, that would be best to use also. (It is configurable, but I never needed to do it, and for someone new to maven it is counter-productive.)

src/
    main/                            // For the application code
        java/
             (com/lva/nbc/...')
        resources/
             (images/)
    test/                            // For unit tests
        java/
        resources/

In this way the jar/war/ear file / class path will contain the paths I put in (...).

getClass().getResourceAsStream("/images/nbct.jpg")
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138