带有 imageview 的 Java Runnable jar 不显示 GUI

标签 java image javafx jar runnable

我正在尝试使我的项目成为可运行的 jar,但问题是 gui 没有显示。从我的 cmd 中的错误来看,我知道这是关于我的 ImageView 的问题。

我的项目包含位于源文件夹之外的另一个文件夹中的 ImageView 。我已经设置了构建路径,但它似乎仍然不起作用

这是我将图像添加到 GUI 的位置:

String path = "./imageTest/";
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) { <----- Line 51
            ImageView imageView;
            imageView = createImageView(file);
            tile.getChildren().addAll(imageView);
        }

文件夹 imageTest 已作为源文件夹添加到构建路径中

这是我通过cmd运行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(JarRsrcLoader.java:58)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
        at application.guifx.MainScreen.mainScene(MainScreen.java:51)
        at application.guifx.MainApp.start(MainApp.java:37)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more"

这是主应用程序

public class MainApp extends Application {
    private Service service;
    private Stage stage;
    private ScrollPane root = new ScrollPane();

    private MainScreen mainScreen; 

    public MainApp() {
        service = Service.getService();
        StorageInitializer.initStorage();

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        service = Service.getService();

        stage = primaryStage;

        mainScreen = new MainScreen(stage);

        stage.setWidth(100);
        stage.setHeight(100);

        primaryStage.setWidth(1000);
        primaryStage.setHeight(850);

        Scene scene = mainScreen.mainScene();<----- Line 37

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    }

}

要明确的是...该程序在通过 eclipse 启动时可以运行,但当我将其设为可运行的 jar 时则不能运行 这是从 eclipse 打开系统时的样子 Link

最佳答案

您的变量listOfFiles为空。请参阅:https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles--

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

  • 检查变量 path 是否使用现有目录进行初始化。
  • 检查您的可执行文件是否有权读取该目录。
  • 检查目录是否包含文件

关于带有 imageview 的 Java Runnable jar 不显示 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43440072/

相关文章:

javascript - HTMLImageElement - src 作为流

java - 在 FXML 中传递数组参数

JavaFX:在运行时更改应用程序语言

java - 同步两个 JavaFx WebView 的滚动条

java - Spring MVC 堆栈溢出错误 - 尝试从数据库获取数据

Java 运行时错误 : java. lang.ClassCastException : main. Scrapper$aktie 无法转换为 java.lang.Comparable

java - 从 Java 1.5 移植到 1.6 时如何调试?

image - 如何使用空间掩码限制光栅处理范围?

java - 自定义ListView不显示任何内容

jquery - 通过 AJAX 加载内容和预加载图像?