I have a simple JavaFX project. It consists from:
- src/app/Main.java
- src/app/controller/MainController.java
- src/app/controller/SecondaryWindowController.java
- src/app/view/Main.fxml
- src/app/view/SecondaryWindow.fxml
The controller MainController.java has method (handler of press button):
public void openWindow(ActionEvent event) {
Pane root = null;
try {
System.out.println("URL: " + getClass().getResource("../view/SecondaryWindow.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/SecondaryWindow.fxml") );
root = loader.load();
}
catch(Exception e) {
e.getMessage();
e.printStackTrace();
}
if (root != null) {
Scene scene = new Scene(root,400,100);
Stage secStage = new Stage();
secStage.setTitle("Secondary Window");
secStage.setScene(scene);
secStage.show();
}
}
Code getClass().getResource("../view/SecondaryWindow.fxml") return right URL if I run project from compile files. But it return null if I run project from JAR-file.
I compile files using:
javac -d bin src\app\Main.java
javac -d bin src\app\controller\*.java
XCOPY src\app\view\*.fxml bin\app\view /s /e /d /y
and run compile files using:
java -classpath bin app.Main
Catalog bin has similar structure of files:
- bin/app/Main.class
- bin/app/controller/MainController.class
- bin/app/controller/SecondaryWindowController.class
- bin/app/view/Main.fxml
- bin/app/view/SecondaryWindow.fxml
And I create JAR-file using:
jar cvfm SimpleFXMLApp.jar manifest.mf -C bin app
Manifest.mf
Manifest-Version: 1.0
Created-By: Denys Selivanov
Main-Class: app.Main
Why relative path doesn't work in JAR-file and getClass().getResource("../view/SecondaryWindow.fxml") return null ? How repair that?
One of the problem's solves is keep controllers and views files in one catalog and use relative path "SecondaryWindows.fxml", but I want keep my structure files of project.
And you can download my project here. This link will be access 30 days.