Javafx FXML文件无法';这项工程建成后就找不到了
为了学习JavaFX,我使用IntelliJ IDEA创建了我的第一个项目,并将该项目作为Eclipse项目导出。当我在这两个IDE中运行项目时,一切都很好。不幸的是,在构建项目并尝试运行.jar或.exe文件后,我遇到了一个异常,我认为问题在于FXMLLoader无法找到fxml文件Javafx FXML文件无法';这项工程建成后就找不到了,javafx,jar,fxml,fxmlloader,Javafx,Jar,Fxml,Fxmlloader,为了学习JavaFX,我使用IntelliJ IDEA创建了我的第一个项目,并将该项目作为Eclipse项目导出。当我在这两个IDE中运行项目时,一切都很好。不幸的是,在构建项目并尝试运行.jar或.exe文件后,我遇到了一个异常,我认为问题在于FXMLLoader无法找到fxml文件 public void initRootLayout() { try { // Load root layout from fxml file. F
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
// Give the controller access to the main app.
RootLayoutController rootLayoutController = loader.getController();
rootLayoutController.setMain(this);
} catch (IOException e) {
showExceptionDialog(e);
}
// Try to load last opened person file.
File file = getPersonFilePath();
if (file != null) {
loadPersonDataFromFile(file);
}
}
我不认为您的fxml文件与jar一起打包。您应该使用
resources
文件夹。转到项目设置并设置模块的资源文件夹。我应该在哪里添加或找到此资源
文件。我在生成目录中已经有一个资源
文件。我应该用它吗?我应该编辑代码中的路径吗?@MustaphaELKOJJI在重新构造项目之前检查jar文件的内容。我不认为这是个问题(或者至少不是整个问题)。你不必把非Java资源放在一个单独的文件夹中,你所要做的就是确保在构建项目时部署它们。(我从不使用资源文件夹,我只是觉得将FXML文件与相应的控制器源代码放在同一个文件夹中更方便。)但是,您必须使用正确的资源名称,和。
不是有效的资源名称。即使将。/view/RootLayout.fxml
替换为/com/melkojji/view/RootLayout.fxml
,问题仍然存在。您可以尝试使用Netbeans。
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(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(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(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.melkojji.controller.Main.initRootLayout(Unknown Source)
at com.melkojji.controller.Main.start(Unknown Source)
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
Exception running application com.melkojji.controller.Main