JavaFX .JAR 文件不会在 ubuntu 20.04 上从终端 运行

JavaFX .JAR file won't run on ubuntu 20.04 from terminal

我正在尝试将我的 javafx 程序构建到 .jar 文件中。当我从 intelliJ 中 运行 时,程序 运行 没问题,但不会从 .jar 文件中 运行。我正在使用带有 java11 和 javafx 版本 11 的 intelliJ。我按照所有说明正确地将项目构建到 .jar 文件中,但是当我 运行 我的程序时,我遇到了一些奇怪的错误(见下文) .我还检查了 .jar 文件中的 javafx 库,它就在那里,所以我不知道为什么会出现此错误。

编辑:我在 Windows 和 ubuntu 上都试过了,但仍然出现错误。我曾经使用 intelliJ 使用 artefact 功能创建 jar 文件。我确保在构建工件时附加 javafx(外部库)。

[Main.java]

package app;

import javafx.fxml.FXMLLoader;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.util.Objects;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("ui/index.fxml")));
        primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream("data/NUIMLogoIcon.png"))));
        primaryStage.setScene(new Scene(root, 740, 600));
        primaryStage.show();
    }

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

[Launcher.java]

import app.Main;

public class Launcher {
    public static void main(String[] args) {
        Main.main(args);
    }
}

[index.fxml]

<ScrollPane focusTraversable="false"
            hbarPolicy="NEVER"
            maxHeight="-Infinity"
            maxWidth="720.0"
            minHeight="-Infinity"
            minWidth="-Infinity"
            prefHeight="1383.0"
            prefWidth="720.0"
            xmlns="http://javafx.com/javafx/11"
            xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="app.ui.Controller"><!--   LINE 40-->
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="1374.0" prefWidth="720.0">
         <children>
         

[错误]

ninja@ninja-lat5410:~/Desktop/MULECodeLab/out/artifacts/MULECodeLab_jar$ java -jar MULECodeLab.jar 
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: javafx.fxml.LoadException: 
file:/home/ninja/Desktop/MULECodeLab/out/artifacts/MULECodeLab_jar/MULECodeLab.jar!/app/ui/index.fxml:40

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at app.Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop(GtkApplication.java:277)

[控制器] 移除

[项目回购] 移除

首先,您的 FXML 没问题。还有其他问题... HTMLDataStorage.java 中有错误,我刚刚做了一行... css = "../../style.css"; 并将 style.css 放入应用程序包文件夹中 - 修复了 java.nio.file.NoSuchFileException 关于 style.css 未找到的错误。

下一步...ScriptsDataStorage.java,这行不通。这是我的快速修复。也许你可以做得更好:

public List<Regex> getPredefinedRegex() {

        ArrayList<String> comments = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader("../../data/comments.txt"))) {
            while (br.ready()) {
                comments.add(br.readLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ArrayList<String> regex = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader("../../data/regex.txt"))) {
            while (br.ready()) {
                regex.add(br.readLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //String[] comments = Util.readlines("comments.txt").split("\n");
        //String[] regex = Util.readlines("regex.txt").split("\n");

        // [ASSUMPTION] regex and comments are the same length
        // conversion from java to bash format
        for (int i = 0; i < regex.size(); i++) {
            regex.set(i, regex.get(i).replace("\", "\\"));
        }

        int size = Math.min(comments.size(), regex.size());
        List<Regex> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(new Regex(regex.get(i), comments.get(i)));
        }
        return  list;
    }

现在,当您从终端 运行 或双击 jar 文件时,它会构建并加载...但它仍然会在终端中吐出很多错误。

comments.txt 和 regex.txt 得到 java.io.FileNotFoundException,我很难解决它。

我知道我完全破坏了你的 Util.readlines()。不过,我使用终端加载了 GUI!你可以忽略我的代码。

关注这个:

你的问题是你构建时的路径不工作