我正在使用Eclipse和JavaFX的Maven Shade插件。 Maven Shade创建了一个可运行的jar,允许我运行我的程序。该程序编译并运行并显示GUI,没有错误通过eclipse。 maven构建运行没有错误并创建jar,当我尝试运行我的jar时会出现问题。
我收到以下错误。
我在这里已经阅读了一些类似的问题,但它们似乎都没有我自己的设置。 Similar这家伙将fxml文件移动到他的资源文件夹中,但我想尝试保留我的项目MVC。还有其他修复吗?
我的问题似乎是MainMenu.fxml中的fx:controller =
错误:引起:java.lang.IllegalStateException:未设置位置。
错误
javafx.fxml.LoadException:
file:/Users/bob/git/Corkscrew/Corkscrew/Corkscrew%20Server/target/Corkscrew-0.0.1-SNAPSHOT.jar!/io/ironbytes/corkscrew/views/MainMenu.fxml:10
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 io.ironbytes.corkscrew.models.MainMenu.start(MainMenu.java:23)
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)
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 io.ironbytes.corkscrew.models.ConnectionsPane.<init>(ConnectionsPane.java:33)
at io.ironbytes.corkscrew.controllers.MainMenuController.<init>(MainMenuController.java:32)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 14 more
main menu.Java
package io.ironbytes.corkscrew.models;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainMenu extends Application {
Parent root;
Stage stage;
@Override
public void start(Stage primaryStage) {
try {
root = FXMLLoader.load(getClass().getClassLoader().getResource("io/ironbytes/corkscrew/views/MainMenu.fxml"));
stage = primaryStage;
stage.setTitle("Welcome");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainMenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.ironbytes.corkscrew.controllers.MainMenuController">
<left>
<ToolBar orientation="VERTICAL" BorderPane.alignment="CENTER">
<items>
<Button mnemonicParsing="false" onAction="#onConnectionsBtnClick" prefHeight="27.0" prefWidth="82.0" text="Clients" />
<Button mnemonicParsing="false" onAction="#onBuilderBtnClick" prefHeight="27.0" prefWidth="83.0" text="Builder" />
<Button mnemonicParsing="false" prefHeight="27.0" prefWidth="83.0" text="Crypter" />
<Button mnemonicParsing="false" onAction="#onCorkscrewBtnClick" text="Corkscrew" />
<Button fx:id="listenBtn" mnemonicParsing="false" onAction="#onListenBtnClick" prefHeight="27.0" prefWidth="82.0" text="Listen" textFill="#e43333" />
<TextField prefHeight="27.0" prefWidth="8.0" promptText="Port: 1337" />
</items>
</ToolBar>
</left>
<center>
<ImageView fitHeight="255.0" fitWidth="348.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
<image>
<Image url="@../../../../img/logo.png" />
</image>
</ImageView>
</center>
</BorderPane>
我终于找到了解决方案。经过几个小时的调试,使用pom.xml文件并更改路径和文件结构,我意识到问题是jar无法加载或者不喜欢位于fxml文件中的fx:controller。所以我需要加载控制器并将其链接到fxml文件而不使用fx:controller =
解:
删除fx:contorller =“io.ironbytes.corkscrew.view.MainMenu.fxml”
并更改加载控制器的方式:
try {
FXMLLoader loader = new FXMLLoader();
loader.setController(new MainMenuController());
loader.setLocation(getClass().getResource("/io/ironbytes/corkscrew/views/MainMenu.fxml"));
root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}`