Issue
I'm trying to create a java FX application and depending on the path of a .java file, a resource from the resource folder can or cannot be loaded. (I dont think this issue is java FX related btw).
This is my project strucure:
java/com/example/app/
-File1.java
-subdirectory/
-File2.java
resources/com/example/views/
-View1.fxml
-View2.fxml
In File1, by using the following line of code to load the resource, the file loads successfully.
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Views/View1.fxml"));
However, in File2 the resource can't be located.
If I move File2 to the parent directory I does work, but is not the solution I'm looking for.
Can someone explain why? Thx
Solution
Solution:
The reason is that you are passing a relative file path as argument, instead of an absolute file path. This is not recommended.
Your path should look like this:
String fxmlPath="/com/example/views/View1.fxml";
FXMLLoader(getClass().getResource(fxmlPath));
Note:
If you are using IntelliJ and you write the first / of your path, you can press CTRL+SPACE to autocomplete your path from here on. Maybe this works in other IDEs too.
Answered By - David Weber
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.