如何使用maven项目和javafx获取资源文件

问题描述 投票:0回答:3

这是我的结构:stupid stack overflow won't let me post this image

在我的主要课程中,我称之为:

public class Main extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource("Form.fxml"));
      primaryStage.setTitle("Hello World");
      primaryStage.setScene(new Scene(root));
      primaryStage.setResizable(false);
      primaryStage.show();
   }
}

但这会产生巨大的错误:

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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/myname/Dropbox/IntellijWorkspace/Somefoodplus/target/classes/Form.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at Main.start(Main.java:11)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.NullPointerException
    at SomeFoodPlus.FXML.Controller.initialize(Controller.java:22)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application Main

我真的不知道我做错了什么,因为我的FXML文件在我的资源文件夹里面,而我的Main.class在我的java包里面。我很确定它与该项目是一个maven项目有关,但我不确定它可能是什么。请帮我解决这个问题,我已经在线寻找解决方案,似乎没有人做maven + fxml项目。

java maven javafx javafx-8
3个回答
0
投票

Jonathan Rosenne所说的基本上是正确的,如果你尝试getResource("/Form.fxml"),它应该工作。这是因为Class.getResource使用包相对路径,而不是绝对路径。例如,如果Form.fxml位于resources/<packages>/Form.fxml,那么您现在拥有的代码将起作用。

另一种选择是使用getClass().getClassLoader().getResource,它始终采用绝对路径。

关于这两种方法之间的差异,已经有一个StackOverflow thread


2
投票

试试“/Form.fxml”。见the Java Class API


0
投票

试试以下:

URL formUrl = Main.class.getClassLoader().getResource("Form.fxml");
Parent root = FXMLLoader.load(formUrl );
© www.soinside.com 2019 - 2024. All rights reserved.