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

Java 尝试执行JAR文件时未找到无效URL或资源,java,netbeans,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.KeyF

我正在使用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);
    }

}
package-digitalpictureframe;
导入javafx.application.application;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.image.ImageView;
导入javafx.stage.stage;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.util.Duration;
导入javafx.animation.animation;
导入javafx.scene.image.image;
导入javafx.scene.layout.VBox;
/**
*
*@作者
*/
公共类DigitalPictureName扩展了应用程序{
//变数
private final static int IMAGE_COUNT=6;//最大数组大小
private int count=0;//初始化计数
Image[]cats=新图像[Image_COUNT];//初始化数组大小
/**
*
*@param阶段
*/
@凌驾
公众假期开始(阶段){
//将图像加载到阵列中
对于(int i=0;i=猫的长度){
计数=0;
}
//更新imageView中显示的图像
setImage(cats[count]);
}
}
//构建关键帧
持续时间秒=新的持续时间(2000);
ImageHandler image=新的ImageHandler();
关键帧=新关键帧(秒,图像);
//构建时间线
时间线=新时间线(关键帧);
timeline.setCycleCount(Animation.unfinite);
//创建场景
场景=新场景(vBox);
//搭建舞台
舞台。片名(“数码相框”);
舞台场景;
stage.show();
timeline.playFromStart();
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
发射(args);
}
}
加载到cats阵列中的图像位于名为“img”的src文件夹中,编号为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
应用程序启动方法中出现异常 java.lang.reflect.InvocationTargetException 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源) 位于java.lang.reflect.Method.invoke(未知源) 位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389) 位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源) 位于java.lang.reflect.Method.invoke(未知源) 位于sun.launcher.launchelper$FXHelper.main(未知源) 原因:java.lang.RuntimeException:应用程序启动方法中的异常 位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:917) 在com.sun.javafx.application.launchempl.lambda$launchApplication$155(launchempl.java:182) 位于java.lang.Thread.run(未知源) 原因:java.lang.IllegalArgumentException:无效URL:无效URL或未找到资源 位于javafx.scene.image.image.validateUrl(image.java:1118) 在javafx.scene.image.image.(image.java:620) 在DigitalPictureName.DigitalPictureName.start(DigitalPictureName.java:37)上 在com.sun.javafx.application.launchempl.lambda$launchApplication1$162(launchempl.java:863) 位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 位于com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 位于java.security.AccessController.doPrivileged(本机方法) 位于com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) 位于com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 还有一个 原因:java.lang.IllegalArgumentException:找不到无效的URL或资源 位于javafx.scene.image.image.validateUrl(image.java:1110) ... 还有11个 运行应用程序DigitalPictureName.DigitalPictureName时出现异常
我假设问题是JAR文件无法访问图像文件,但我不知道如何修复它。如何将图像加载到JAR文件中?

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

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

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