1

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!

lpsweder
  • 111
  • 8
  • 2
    Possible duplicate of [Why relative path doesn't work in JAVA in JAR-file?](https://stackoverflow.com/questions/45765092/why-relative-path-doesnt-work-in-java-in-jar-file) – Vinay Prajapati Jan 25 '19 at 09:31
  • Eclipse had a problem with locations relative to the package. I recommend using absolute paths instead, e.g. `Main.class.getResource("/pictures/forms_logo_2_1.png")`. If this does not work, verify the picture is included in the jar either by using a zip tool to browse the contents or by using `jar -tf myapplication.jar` to get a list of the contents. – fabian Jan 25 '19 at 11:40
  • @fabian thank you! Yeah I noticed that. So I tried to fixed it by creating the folder ‘src/main/resources/pictures’ by hand in the file system of the project. Now the pictures are included in the jar. But the problem still exists ‍♂️ So I will try to create a new Maven Project and migrate everything from the old project. Maybe the converting of the old project from a java project to a Maven project was the reason for the whole problem! – lpsweder Jan 25 '19 at 11:55

1 Answers1

0

I find a solution on my own. There was a problem with the file system of the maven project and the arrangement of the code. The error occurred by using the eclipse feature in [right click] > configure > convert to maven project. This caused some missing folders in the file system of the program. Now it's possible to simply reference the files.

The solution is to create a new Maven Project and migrate everything from your old project. Than it worked for me. Cheers!

lpsweder
  • 111
  • 8