gpt4 book ai didi

转换后的 Maven 项目中文件的 JavaFx InvocationTargetException

转载 作者:太空宇宙 更新时间:2023-11-03 17:05:02 25 4
gpt4 key购买 nike

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

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

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

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 () 来自对象类型

关于这一点,我还尝试了 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 项目并将结果发布在这里。

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

最佳答案

我自己找到了解决办法。 maven项目的文件系统和代码的排列有问题。错误发生在[右键单击] > configure > convert to maven project中使用eclipse特性。这导致程序的文件系统中缺少一些文件夹。现在可以简单地引用文件。

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

关于转换后的 Maven 项目中文件的 JavaFx InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com