请参阅底部的 TL;DR。
嗨, 所以我认为这是一个非常基本的事情,但它让我头疼:(
我正在编写一个包含一些图像的 JavaFX 应用程序。我正在使用 gradle 来构建项目。 我的结构是这样的:
src
- main
-- java
--- view
---- sections
----- WelcomeSection.java
-- resources
--- logo.png
- tests
--java
--resources
遵循 gradle 包结构后,在 WelcomeSection.java 中我有以下代码片段(记住它是一个 JavaFX 应用程序):
Image logo = new Image(getClass().getResourceAsStream("../../../resources/logo.png"));
ImageView logoImageView = new ImageView(logo);
...当我使用 Eclipse 编译和运行时,效果非常好。
当我尝试通过 gradle 运行构建时,它会生成 jar 文件。运行 jar 文件后,我收到以下运行时异常:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Unknown Source)
at javafx.scene.image.Image.<init>(Unknown Source)
at main.java.view.sections.WelcomeSection.logoLabel(WelcomeSection.java:83)
at main.java.view.sections.WelcomeSection.setupTopBar(WelcomeSection.java:73)
at main.java.view.sections.WelcomeSection.setup(WelcomeSection.java:61)
at main.java.view.sections.WelcomeSection.<init>(WelcomeSection.java:52)
at main.java.view.MainView.setupWelcomeSection(MainView.java:27)
at main.java.view.MainView.setup(MainView.java:37)
at main.java.view.MainView.<init>(MainView.java:23)
at main.java.Main.start(Main.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
... 1 more
...这显然与图像路径有关。当我删除图像时,使用 gradle 构建时效果很好。
TL;DR:如何在 Javafx 应用程序中正确引用图像,以便它们在 Eclipse 中工作,并且更重要的是在使用 gradle 构建时?我尝试更改 src/main/resources/logo.png 的 Java 代码中的路径,但这仍然不适用于 gradle,也不适用于在 Eclipse 中运行。
最佳答案
如果您的文件直接位于 src/main/resources
下,则意味着它位于类路径的根级别,因此它应该是 getClass() .getResourceAsStream("/logo.png")
如下:
Image logo = new Image(getClass().getResourceAsStream("/logo.png"));
ImageView logoImageView = new ImageView(logo);
但是请注意,构造函数Image(String url)
根据url
的格式可以尝试从上下文ClassLoader
获取图像code> 因此您应该能够简单地获得相同的结果:
Image logo = new Image("/logo.png");
ImageView logoImageView = new ImageView(logo);
关于java - 使用 gradle 构建项目时图像产品空指针 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40948410/