Skip to content
Advertisement

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

I’m trying to build my javafx program into a .jar file. The program runs fine when i’m running it from intelliJ but won’t run from .jar file. I’m using intelliJ with java11 and javafx version 11. I followed all the instructions correctly to build the project into a .jar file but when i run my program i get some strange error (see below). I also checked javafx library in the .jar file and it’s there so i don’t know why i’m getting this error.

edit: I tried both on Windows and ubuntu but was still getting the error. I used to intelliJ to create the jar file using artefact feature. I made sure to attach javafx (external library) when building the artifact.

[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>
         

[ERROR]

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$2(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$9(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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$11(GtkApplication.java:277)

[Controller] removed

[Project repo] removed

Advertisement

Answer

First, your FXML is fine. There are other issues… There’s an error in HTMLDataStorage.java, I just made the line… css = "../../style.css"; and put style.css in the app package folder – that fixes the java.nio.file.NoSuchFileException error about the style.css not being found.

Next… ScriptsDataStorage.java, this ain’t working. This was my quick fix. Maybe you can do something better:

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;
    }

It now builds and loads when your run it from the terminal or double click on the jar file… but it spits out lots of errors in the terminal still.

It’s getting java.io.FileNotFoundException for comments.txt and regex.txt and I’m having great difficulty solving it.

I see I’ve completely sabotaged your Util.readlines(). I got the GUI to load using the terminal though! You can ignore my code.

Focus on this:

Your problems are your paths when you build aren’t working

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement