0

I wrote an application in eclipse, exported it with libraries extracted, compiled it into an exe using launch4j. It runs fine, but only when the main.jar is in the same parent folder. I'm not sure if it's something from launch4j messing it up, or if it's elsewhere. I've included a sample of the code

jvm options --module-path /users/fishe/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml -Djavafx.verbose=true -jar main.jar

SAMPLE

package src;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Testing extends Application implements Serializable {
    private static final long serialVersionUID = 8659236541611368787L;
    
    protected Button buttonFileSave = new Button("Save");
    protected Button buttonPrevious = new Button("<");
    protected Button buttonNext = new Button(">");
    
    protected TextArea textFileData = new TextArea();
    protected TextField textFileMark = new TextField();
    protected VBox boxMain = new VBox();
    protected HBox boxToolbar = new HBox();
    protected Scene primaryScene = new Scene(boxMain);
    protected Stage primaryStage;
    protected File fileDirectory = new File(System.getProperty("user.home") + "/Documents/fileEditor/data");
    protected File fileDirectoryData = new File(System.getProperty("user.home") + "/Documents/fileEditor/data");

    protected List<String> fileData;
    protected List<String> fileMark;
    protected int fileCount;
    protected int file;
    
    protected static class Data implements Serializable {
        private static final long serialVersionUID = 17L;
        protected List<String> fileData;
        protected List<String> fileMark;
        protected int fileCount;
        protected int lastFile;
    }
    
    public void start(final Stage newStage) {
        primaryStage = newStage;
        primaryStage.setScene(primaryScene);
        primaryStage.show();
        boxMain.getChildren().addAll(textFileMark, textFileData, boxToolbar);
        boxToolbar.getChildren().addAll(buttonPrevious, buttonFileSave, buttonNext);
        
        buttonPrevious.setOnAction(event -> {
            fileData.set(file, textFileData.getText());
            fileMark.set(file, textFileMark.getText());
            if (file > 0) {
                file -= 1;
            }
            
            textFileData.setText(fileData.get(file));
            textFileMark.setText(fileMark.get(file));
        });
        
        buttonNext.setOnAction(event -> {
            if (fileCount == file) {
                fileData.add("");
                fileMark.add("");
                fileCount += 1;
            }
            file += 1;
            
            textFileData.setText(fileData.get(file));
            textFileMark.setText(fileMark.get(file));
        });
        
        buttonFileSave.setOnAction(event -> {
            fileData.set(file, textFileData.getText());
            fileMark.set(file, textFileMark.getText());
            
            final Data saveData = new Data();
            saveData.fileData = fileData;
            saveData.fileMark = fileMark;
            saveData.fileCount = fileCount;
            saveData.lastFile = file;
            
            save(saveData);
        });
        
        fileDirectory.mkdirs();
        if (!fileDirectoryData.isFile()) {
            final Data saveData = new Data();
            
            saveData.fileData = new ArrayList<>();
            saveData.fileMark = new ArrayList<>();
            saveData.fileData.add("");
            saveData.fileMark.add("");
            saveData.lastFile = 0;
            saveData.fileCount = 0;
            
            save(saveData);
        }
        final Data loadData = (Data) load();

        fileMark = loadData.fileMark;
        fileData = loadData.fileData;
        file = loadData.lastFile;
        fileCount = loadData.fileCount;
        
        textFileData.setText(fileData.get(file));
        textFileMark.setText(fileMark.get(file));
    }
    
    protected Object load() {
        Object oisReturn = null;
        try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(fileDirectoryData.toString())))) {
            oisReturn = ois.readObject();
        } catch(Exception e) {
            System.out.println("Error on load: " + e);
            e.printStackTrace();
        }
        return oisReturn;
    }
    
    protected void save(final Serializable data) {
        try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(fileDirectoryData.toString())))) {
            oos.writeObject(data);
        } catch(Exception e) {
            System.out.println("Error on save: " + e);
            e.printStackTrace();
            return;
        }
    }
    public static void main(final String[] args) throws Exception{
        launch(args);
    }
}

  • I don’t know launch4j, so cannot help you with that. If you can’t solve your issue with your current approach, as an alternative, you could use [warp packer to pack your JavaFX app into an exe](https://stackoverflow.com/questions/69811401/how-to-create-a-standalone-exe-in-java-that-runs-without-an-installer-and-a-jr). – jewelsea Dec 08 '21 at 18:58
  • you can alternatively use jpackage which is part of the jdk. https://docs.oracle.com/en/java/javase/14/jpackage/packaging-overview.html – Jawad El Fou Dec 09 '21 at 01:16
  • how do the jvm argumets work? cause i need to include some javafx modules, but do they work if they're not accessible by the program (i.e. on another computer)? – protocol_1903 Dec 09 '21 at 01:28
  • I am not quite sure I understand your question but `/users/fishe/javafx-sdk-11.0.2/lib` is an absolute path to a folder in a user directory. The folder won’t be available on another computer or, usually, accessible by another user on the same computer. – jewelsea Dec 10 '21 at 05:10

0 Answers0