尝试执行 JAR 文件时找不到无效的 URL 或资源



我正在使用NetBeans IDE 8.2。这是我正在使用的代码:

package digitalpictureframe;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;
import javafx.animation.Animation;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
/**
 *
 * @author
 */
public class DigitalPictureFrame extends Application {
    //Variables
    private final static int IMAGE_COUNT = 6;       //max array size
    private int count = 0;                          //initialize count
    Image[] cats = new Image[IMAGE_COUNT];          //initialize array size
    /**
     *
     * @param stage
     */
    @Override
    public void start(Stage stage) {
        //load images into array
        for (int i = 0; i < cats.length; i++) {
            cats[i] = new Image("img\" + (i + 1) + ".jpg");
        }
        //create vbox pane
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        ImageView imageView = new ImageView(cats[0]);
        vBox.getChildren().add(imageView);
        //handler class
        class ImageHandler implements EventHandler<ActionEvent> {
            @Override
            public void handle(ActionEvent event) {
                    //increment count variable
                    count++;
                    //reset image index to beginning
                    if (count >= cats.length) {
                        count = 0;
                    }
                    //update image displayed in imageView
                    imageView.setImage(cats[count]);
                }
            }
        //build keyframe
        Duration seconds = new Duration(2000);
        ImageHandler image = new ImageHandler();
        KeyFrame keyFrame = new KeyFrame(seconds, image);
        //build timeline
        Timeline timeline = new Timeline(keyFrame);
        timeline.setCycleCount(Animation.INDEFINITE);
        //create scene
        Scene scene = new Scene(vBox);
        //set up stage
        stage.setTitle("Digital Picture Frame");
        stage.setScene(scene);
        stage.show();
        timeline.playFromStart();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        launch(args);
    }
}

加载到 cats 数组中的图像位于 src 文件夹中一个名为"img"的文件夹中,编号为 1-6。当我从 NetBeans IDE 运行该程序时,它运行良好;但是,当我尝试在构建项目后创建的 dist 文件夹中执行 JAR 文件时,它根本不运行。当我尝试使用命令行运行它时,我得到这个:

Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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 sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1118)
    at javafx.scene.image.Image.<init>(Image.java:620)
    at digitalpictureframe.DigitalPictureFrame.start(DigitalPictureFrame.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
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
    ... 11 more
Exception running application digitalpictureframe.DigitalPictureFrame

我假设问题是JAR文件无法访问图像文件,但是我不知道如何解决它。如何将图像实际加载到 JAR 文件中?

我假设图像已正确放置在JAR中(因此请解压缩您的JAR,看看它们是否真的存在(。

如果有,那么您应该能够像这样加载它们:

new Image(Class.getResourceAsStream("/img/" + (i + 1) + ".jpg"));

最新更新