Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load fxml file located in "src" maven folder

Tags:

java

maven

javafx

I am working on a JavaFX 8 (maven) project. I want to store an fxml file inside of sources (not inside resources) folder.
When I store my fxml to location /src/main/resources/views/b/MyFxml.fxml I am loading it without errors using the command,

new FXMLLoader(getClass().getResource("/views/b/MyFxml.fxml"));

Is there any way to load my fxml file from /src/main/java/package/name/RoleView.fxml location?

like image 517
Georgios Syngouroglou Avatar asked Jan 07 '23 14:01

Georgios Syngouroglou


1 Answers

Java does not make distinction between resources and sources. As long as your fxml file is visible in classpath (packaged into *.jar appropriately) you can reach it the same way. Given that you use maven, this is the configuration you need:

<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.fxml</include>
        </includes>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

Then, this line should work to load your file:

new FXMLLoader(getClass().getResource("/package/name/RoleView.fxml"));
like image 139
bezmax Avatar answered Jan 15 '23 03:01

bezmax