For context, I have a Maven Project Structure in Intellij and also the /target/classes/
The code where I am getting error:
@Override
public void start(Stage stage) throws IOException {
Parent root;
FXMLLoader fxmlLoader = new FXMLLoader();
// Below line has the problem of loading image inside fxml file.
root = fxmlLoader.load(getClass().getClassLoader().getResourceAsStream("AuthorizationPage.fxml"));
// not used in code but below line returns inputstream object which is not null so the image loads fine
when independently verified.
// System.out.println(Objects.isNull(getClass().getClassLoader().getResourceAsStream("sign-up-logo.png")));
//Below Line works fine and loads fxml file correctly with images.
root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/AuthorizationPage.fxml")));
Scene scene = new Scene(root);
stage.setScene(scene);
SceneManager sceneManager = SceneManager.getInstance(stage);
sceneManager.addScene("/AuthorizationPage.fxml", "User Authorization");
if (!SessionManager.hasUserSessionFromLocalFileExpired()) {
sceneManager = SceneManager.getInstance(stage);
sceneManager.addScene("/convertor.fxml", "Convert Pdf Documents");
sceneManager.activateScene("Convert Pdf Documents", "Convert Pdf Documents", true, true);
} else {
sceneManager.activateScene("User Authorization", "User Authorization", false, false);
}
}
The stacktrace for the exception:
null/images/sign-up-logo.png
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics@19.0.2/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics@19.0.2/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@19.0.2/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics@19.0.2/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: javafx.fxml.LoadException:
unknown path:33
at javafx.fxml@19.0.2/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
at javafx.fxml@19.0.2/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2692)
at javafx.fxml@19.0.2/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2539)
at com.javafxapp.javafxapplication/com.app.convertor.authorization.Main.start(Main.java:39)
at javafx.graphics@19.0.2/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@19.0.2/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@19.0.2/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@19.0.2/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@19.0.2/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics@19.0.2/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@19.0.2/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics@19.0.2/javafx.scene.image.Image.validateUrl(Image.java:1138)
at javafx.graphics@19.0.2/javafx.scene.image.Image.<init>(Image.java:695)
at javafx.fxml@19.0.2/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at javafx.fxml@19.0.2/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml@19.0.2/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:774)
at javafx.fxml@19.0.2/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2961)
at javafx.fxml@19.0.2/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2646)
... 11 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics@19.0.2/javafx.scene.image.Image.validateUrl(Image.java:1123)
... 17 more
Exception running application com.app.convertor.authorization.Main
Process finished with exit code 1
fxml snippet:
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<ImageView fitHeight="108.0" fitWidth="101.0" layoutX="270.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@images/sign-up-logo.png" />
</image>
</ImageView>
</children>
</AnchorPane>
</children>
</HBox>
Maven pom file snippet:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources/com/app/convertor/config</directory>
</resource>
<resource>
<directory>src/main/resources/com/app/convertor/fxml/images</directory>
</resource>
<resource>
<directory>src/main/resources/com/app/convertor/fxml</directory>
</resource>
</resources>
Why the image fails to load with getResourcesAsStream()
method but works fine using getResources()
method? I checked if getResourcesAsStream()
returns null
, but it identifies fxml file correctly from resource folder. What I am getting wrong and what should I do to load fxml file correctly? There are similar questions asked but none of them helped me.