Issue
I'm using NetBeans IDE 8.2. This is the code I'm using:
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);
}
}
The images being loaded into the cats array are located in the src folder in a folder named "img," numbered 1-6. When I run the program from the NetBeans IDE, it runs perfectly; however, when I try to execute the JAR file in the dist folder created after building the project, it does not run - at all. When I try to run it using the command line, I get this:
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
I'm assuming the issue is that the JAR file can't access the image files, but I can't figure out how to fix it. How do I actually load the images into the JAR file?
Solution
I'm assuming the Images are correctly placed into the JAR (so please unzip your JAR, and see if they are actually there).
If they're are, then you should be able to load them like this:
new Image(Class.getResourceAsStream("/img/" + (i + 1) + ".jpg"));
Answered By - john16384