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로 빌드 할 때 제대로 작동합니다.
요약 : 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
있으므로 다음과 같이 간단히 동일한 결과를 얻을 수 있습니다.
Image logo = new Image("/logo.png");
ImageView logoImageView = new ImageView(logo);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다