我正在与Eclipse和JavaFX一起使用Maven Shade插件。Maven Shade创建一个可运行的jar,允许我运行我的程序。该程序编译和运行,并显示GUI没有任何错误通过eclipse。maven构建在没有错误的情况下运行,并创建jar,当我试图运行jar时,问题就出现了。
我得到以下错误。
我在这里读过几个类似的问题,但他们似乎都没有和我一样的设置。Similar这个家伙将fxml文件移到他的资源文件夹中,但是我想尝试保留我的项目MVC。还有别的办法吗?
我的问题似乎是“fx:controller= in MainMenu.fxml”
错误:由: java.lang.IllegalStateException: Location引起的。
误差
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 moreMainMenu.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>发布于 2018-01-04 17:56:47
我终于找到了解决办法。经过几个小时的调试、处理pom.xml文件以及更改路径和文件结构,我意识到问题在于jar无法加载或不喜欢位于fxml文件中的fx:控制器。因此,我需要加载控制器并将其链接到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();
}
}`https://stackoverflow.com/questions/48087268
复制相似问题