簡體   English   中英

嘗試執行JAR文件時找不到無效的URL或資源

[英]Invalid URL or resource not found when trying to execute JAR file

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM