JavaFX 创建 Jar 文件错误

JavaFX Create Jar File Error

Parent root = FXMLLoader.load(getClass().getResource("kibAr//kibArPerson.fxml"));

第 12 行 kibAr 包在 src 包中

C:\>java -jar person.jar
Exception in Application start method
Exception in thread "main" 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 org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
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$launchApplication7(
Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda/14832625.run(Unknow
n Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
        at kibAr.AnaEkran.start(AnaEkran.java:18)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13
(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda/25863743.run(Unkno
wn Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(Unknown
 Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda/27949311.run(Unkno
wn Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null4(Unknown Sourc
e)
        at com.sun.javafx.application.PlatformImpl$$Lambda/13569241.run(Unkno
wn Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater5(Unknown S
ource)
        at com.sun.javafx.application.PlatformImpl$$Lambda/31743054.run(Unkno
wn Source)
        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$null1(Unknown Source)
        at com.sun.glass.ui.win.WinApplication$$Lambda/7345883.run(Unknown So
urce)
        ... 1 more

我在 运行 jar 文件未显示 window..

之后使用 Eclipse 创建 jar 文件(导出 > 可运行的 jar 文件)

在我 运行 来自 cmd 的 jar 文件之后,我显示了这个错误。我该如何解决这个问题?

由于您没有展示可重现的示例,并且可能会出现一些问题,这里有一个有效的示例:

创建一个包应用程序并将此 class 放入其中:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/Test.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

在打包应用中创建一个"Test.fxml"。

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

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


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
   <children>
      <Pane layoutX="-125.0" layoutY="-143.0" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="134.0" layoutY="161.0" mnemonicParsing="false" text="Button" />
         </children>
      </Pane>
   </children>
</AnchorPane>

在 Eclipse 中 select

导出 -> 可运行的 JAR 文件 -> 将所需的库提取到生成的 JAR

(当然你需要指定正确的启动配置)

可以执行生成的JAR。

如果你有别的东西,需要帮助就得拿出来