无法使用javafx在intellij之外运行jar

时间:2015-11-17 13:46:32

标签: java intellij-idea javafx

我正在JavaFX成功运行IntelliJ项目,但是当我尝试构建工件并在外面运行jar时,它将无法打开或立即打开和关闭。

我尝试将.fxml文件移动到java目录中并更改负载,但是我收到错误。

我的项目结构如下:

enter image description here

我的主要课程在这里:

public class Main extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
        primaryStage.setTitle("xxxxxxxxxxxxx");
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我选择了我使用的所有jar个库,并将它们添加到工件中。我的猜测是它没有在.fxml之外加载IntelliJ个文件。

如果需要,我的sample.fxml就在这里:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="650.0" prefWidth="550.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <TextField fx:id="username" alignment="CENTER" layoutX="151.0" layoutY="246.0" prefHeight="25.0" prefWidth="229.0" />
        <PasswordField fx:id="password" layoutX="152.0" layoutY="345.0" prefHeight="25.0" prefWidth="229.0" />
        <CheckBox fx:id="rememberMe" layoutX="207.0" layoutY="418.0" mnemonicParsing="false" text="Remember me ?">
            <font>
                <Font size="13.0" />
            </font>
        </CheckBox>
        <Button fx:id="login" layoutX="199.0" layoutY="542.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="153.0" text="OK" textAlignment="CENTER">
            <font>
                <Font size="14.0" />
            </font></Button>
        <Hyperlink layoutX="203.0" layoutY="446.0" text="I forgot my password !" />
        <Label layoutX="239.0" layoutY="219.0" text="Username:">
            <effect>
                <Lighting>
                    <bumpInput>
                        <Shadow />
                    </bumpInput>
                </Lighting>
            </effect>
        </Label>
        <Label alignment="CENTER" layoutX="240.0" layoutY="317.0" text="Password:" textAlignment="CENTER">
            <effect>
                <Lighting>
                    <bumpInput>
                        <Shadow />
                    </bumpInput>
                </Lighting>
            </effect>
        </Label>
    </children>
</Pane>

错误:

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(Unk
nown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Sou
rce)
        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 So
urce)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(
Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at Main.start(Main.java:25)
        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 Sourc
e)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown S
ource)
        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
Exception running application Main

0 个答案:

没有答案