【问题标题】:JavaFx InvocationTargetException for files in converted Maven Project转换后的 Maven 项目中文件的 JavaFx InvocationTargetException
【发布时间】:2019-01-25 08:55:14
【问题描述】:

最近我尝试在 Eclipse 的 JavaFx Maven 项目中使用将图片添加到标签。如果我在 Eclipse 中运行该应用程序,一切正常,不会发生错误。

这篇文章不同于:“为什么相对路径在 JAR 文件中的 JAVA 中不起作用?”邮政!我尝试了这篇文章的解决方案,但它对我不起作用。我在下面描述了这一点。

如果我从java -jar myapplication.jar 之类的 Linux 终端运行应用程序,则会出现以下错误:

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

我将错误定位在图片的文件导入中。对于导入,我使用了以下代码:

FileInputStream input = new FileInputStream("path/picture.png");
Image image = new Image(input);
Label img1 = new Label();
img1.setGraphic(new ImageView(image));

我还尝试将 imageUrl 直接提供给 Image 类,例如 Image image = new Image(imageUrl);。这在我以前的项目中有效。

我也试过Image image = new Image(getClass().getResourceAsStream(imageUrl));,但这里eclipse抛出错误Cannot make a static reference to the non-static method getClass() from the type Object

关于这个我也试过Image image = new Image(Main.class.getResource(imageUrl).toExternalForm();。对我来说,它只能以这种方式执行而不会出错。

更新:我也试过这个:

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));

它会抛出与上面相同的错误。

更新: 好的,看起来可执行 JAR 文件与查找图片和其他数据本身不匹配。例如,图片位于图片文件夹中,而应用程序无法在图片文件夹中找到它。 所以这是一个参考问题。 因为我将它转换为一个 maven 项目,可能结构不正确。我将尝试创建一个新的 maven 项目并将结果发布在这里。

因为这些都是我发现的将图片导入标签的常用方法,所以我有点沮丧。我希望有人能解决这个小问题。干杯!

【问题讨论】:

  • Eclipse 在相对于包的位置存在问题。我建议改用绝对路径,例如Main.class.getResource("/pictures/forms_logo_2_1.png")。如果这不起作用,请使用 zip 工具浏览内容或使用 jar -tf myapplication.jar 获取内容列表来验证图片是否包含在 jar 中。
  • @fabian 谢谢!是的,我注意到了。所以我试图通过在项目的文件系统中手动创建文件夹“src/main/resources/pictures”来修复它。现在这些图片都包含在罐子里了。但是问题依然存在?????????‍♂️所以我会尝试创建一个新的Maven项目并从旧项目中迁移所有内容。也许将旧项目从 java 项目转换为 Maven 项目是整个问题的原因!

标签: java eclipse ubuntu javafx java-8


【解决方案1】:

我自己找到了解决方案。 maven项目的文件系统和代码的排列有问题。使用[right click] &gt; configure &gt; convert to maven project 中的eclipse 功能发生错误。这导致程序的文件系统中缺少一些文件夹。现在可以简单地引用文件。

解决方案是创建一个新的 Maven 项目并从旧项目中迁移所有内容。比它对我有用。干杯!

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2019-07-20
    • 2021-07-11
    相关资源
    最近更新 更多