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

时间:2017-03-26 02:55:18

标签: java netbeans

我使用的是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文件夹中名为&#34; img,&#34;的文件夹中。编号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文件中?

1 个答案:

答案 0 :(得分:0)

我假设图像被正确放入JAR(所以请解压缩你的JAR,看看它们是否真的在那里)。

如果他们是,那么你应该能够像这样加载它们:

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