0

我创建了一个想要转换为 MacOS .app 的 java 应用程序。我设法针对我的 jar 文件运行 java 打包程序,该文件为我创建了 .pkg 文件。安装后应用程序正在启动(预加载器),但.fxml由于“!”而以文件加载问题结束 出于某种原因添加到路径的字符:

javafx.fxml.LoadException: 
file:/Applications/app.app/Contents/Java/app.jar!/programGUI.fxml

当 .jar 文件位于其他位置时,它可以正常工作/Applications/app.app/。我已经尝试从Application/app.app/Contents/Java/and手动启动它Application/app.app/Contents/MacOS/,但结果是相同的(即使使用 sudo)。我厌倦了更改权限(chmod)和系统偏好(system preferences > privacy)但仍然没有运气。

ClassLoader classLoader = this.getClass().getClassLoader();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/programGUI.fxml"));
Parent root = loader.load();

值得一提的是,我以类似的方式为预加载器加载背景图片并且它正在工作。

Image backgroundImage = new Image(classLoader.getResource("splashScreen.png").toString());

任何建议将不胜感激。

编辑:根据要求添加主类

public class Main extends Application {
    private BooleanProperty ready = new SimpleBooleanProperty(false);

    @Override
    public void start(Stage primaryStage) {
        try {
            myStart();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        ready.addListener(new ChangeListener<Boolean>() {
            public void changed(
                    ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
                if (Boolean.TRUE.equals(t1)) {
                    Platform.runLater(new Runnable() {
                        public void run() {
                            try {
                                ClassLoader classLoader = this.getClass().getClassLoader();
                                FXMLLoader loader = new FXMLLoader();
                                loader.setClassLoader(getClass().getClassLoader());
                                loader.setLocation(getClass().getResource("/programGUI.fxml"));
                                Parent root = loader.load();
                                Scene scene = new Scene(root, 900, 600);
                                primaryStage.setScene(scene);
                                primaryStage.setTitle("myName");
                                primaryStage.setResizable(false);
                                primaryStage.getIcons().add(new Image(classLoader.getResource("icon.png").toString()));
                                primaryStage.show();
                            } catch (IOException ex) {
                                System.out.println(ex.getMessage());
                            }
                        }
                    });
                }
            }
        });
    }
    private void myStart() throws Exception {
        Task task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
            FileDownloader fd = new FileDownloader(new URL("www.example.com/myfile.txt"));
                while (fd.status == 0) { //0 = downloading, 2 = completed
                    try {
                        Thread.sleep(50);
                        if (fd.getProgress() > 0) {
                            notifyPreloader(new Preloader.ProgressNotification((fd.getProgress())));
                        }
                        if (fd.status == 100) {
                            ready.setValue(Boolean.TRUE);
                            notifyPreloader(new Preloader.StateChangeNotification(
                                    Preloader.StateChangeNotification.Type.BEFORE_START));
                        }
                    } catch (InterruptedException ex) {
                        System.out.println(ex.getMessage());
                    }
                }
                return null;
            }
        };
        Thread t = new Thread(task);
        new Thread(task).start();
    }

    public static void main(String[] args) {
        LauncherImpl.launchApplication(Main.class, myPreloader.class, args);
    }
}

提前致谢, 皮奥特

4

0 回答 0