Recently I tried to use add a picture to a label in my JavaFx Maven Project in Eclipse. If I run the application in Eclipse everything works fine and no error occurs.
This post is different to the: "Why relative path doesn't work in JAVA in JAR-file?" post! I tried the solution of this post, but it didn't worked for me. I described that below.
If I run the application from the Linux terminal like java -jar myapplication.jar
the following error occurs:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$412(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: src/pictures/forms_logo_2_1.png (Datei oder Verzeichnis nicht gefunden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at de.elastic.client.Main.pane1(Main.java:193)
at de.elastic.client.Main.start(Main.java:259)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$419(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$399(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$397(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$398(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$203(GtkApplication.java:139)
... 1 more
Exception running application de.elastic.client.Main
I localized the error at a file-import of a picture. For the import I used this code:
FileInputStream input = new FileInputStream("path/picture.png");
Image image = new Image(input);
Label img1 = new Label();
img1.setGraphic(new ImageView(image));
I also tried to give the imageUrl directly to the Image class like Image image = new Image(imageUrl);
. This worked in previous projects for me.
I also tried Image image = new Image(getClass().getResourceAsStream(imageUrl));
but here eclipse throws the error Cannot make a static reference to the non-static method getClass() from the type Object
.
Regarding this I also tried Image image = new Image(Main.class.getResource(imageUrl).toExternalForm();
. For me it's only executable in this way without an error.
Update: I also tried this:
ClassLoader classLoader1 = Main.class.getClassLoader();
String ImgUrl1 = classLoader1.getResource("path/picture.png").toExternalForm();
Image image = new Image(ImgUrl1);
Label img1 = new Label();
img1.setGraphic(new ImageView(image));
It's throws the same error as above.
Update: Ok, it seems like the executable JAR-file don't match to find the picture and other data in itself. So for example a picture lies in the pictures folder and the application can't find it in the pictures folder. So it's a reference problem. Cause I converted it to a maven project maybe the structure is not right. I will try to create a new maven project and post the result here.
Because this are all common used ways I've found to import a picture into a label, I'm a bit frustrated. I hope someone have a solution for this tiny problem. Cheers!