java.lang.IllegalArgumentException: Invalid URL or resource not found


Question

I tested this code:

public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {

        // Image
        Image image = new Image("za.png");
        ImageView imageView = new ImageView();
        imageView.setImage(image);

        // Text
        Text t = new Text();
        t.setText("Do you want to quit?");

        // Buttons
        Button btnYes = new Button("Yes");
        Button btnNo = new Button("No");
        btnYes.setStyle("-fx-background-color:\n"
                + "        #090a0c,\n"
                + "        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),\n"
                + "        linear-gradient(#20262b, #191d22),\n"
                + "        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));\n"
                + "    -fx-background-radius: 5,4,3,5;\n"
                + "    -fx-background-insets: 0,1,2,0;\n"
                + "    -fx-text-fill: white;\n"
                + "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );\n"
                + "    -fx-font-family: \"Arial\";\n"
                + "    -fx-text-fill: linear-gradient(white, #d0d0d0);\n"
                + "    -fx-font-size: 12px;\n"
                + "    -fx-padding: 10 20 10 20;");

        btnNo.setStyle("-fx-background-color:\n"
                + "        #090a0c,\n"
                + "        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),\n"
                + "        linear-gradient(#20262b, #191d22),\n"
                + "        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));\n"
                + "    -fx-background-radius: 5,4,3,5;\n"
                + "    -fx-background-insets: 0,1,2,0;\n"
                + "    -fx-text-fill: white;\n"
                + "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );\n"
                + "    -fx-font-family: \"Arial\";\n"
                + "    -fx-text-fill: linear-gradient(white, #d0d0d0);\n"
                + "    -fx-font-size: 12px;\n"
                + "    -fx-padding: 10 20 10 20;");

        // Buttons layout
        HBox hbox = new HBox(8); // spacing = 8
        hbox.setStyle("-fx-padding: 15; -fx-font-size: 15pt;");
        hbox.getChildren().addAll(btnYes, btnNo);
        hbox.setAlignment(Pos.BASELINE_RIGHT);

        BorderPane bp = new BorderPane();
        bp.setStyle("-fx-background-color: linear-gradient(#ffffff,#f3f3f4);\n"
                + "    -fx-border-width: 1 1 1 1;\n"
                + "    -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;\n"
                + "    -fx-font-size: 1.083333em;\n"
                + "    -fx-text-fill: #292929;");

        bp.setPadding(new Insets(10, 20, 10, 20));
        //Button btnTop = new Button("Top");
        bp.setTop(null);
        //Button btnLeft = new Button("Left");
        bp.setLeft(imageView);
        //Button btnCenter = new Button("Center");
        bp.setCenter(t);
        //Button btnRight = new Button("Right");
        bp.setRight(null);
        //Button btnBottom = new Button("Bottom");
        bp.setBottom(hbox);
        Scene scene = new Scene(bp, 500, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

I get this error:

Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run429319394/DX-57DC.jar using platform /opt/jdk1.8.0/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:986)
    at javafx.scene.image.Image.<init>(Image.java:538)
    at com.dx57dc.main.DX57DC.start(DX57DC.java:28)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:979)
    ... 12 more
Java Result: 1
Deleting directory /home/rcbandit/Desktop/test/DX-57DC/dist/run429319394
jfxsa-run:
BUILD SUCCESSFUL (total time: 6 seconds)

I placed the image file next to the java source code file but the file is not found. Can you tell me how to fix this?

1
9
6/7/2013 6:16:32 PM

Accepted Answer

Do

javafx.scene.image.Image image = new javafx.scene.image.Image(getClass().getResource("za.jpg").toExternalForm());
ImageView iv = new ImageView(image);

Or simply

ImageView iv = new ImageView(getClass().getResource("za.jpg").toExternalForm());
15
6/7/2013 6:29:18 PM

Image image = new Image("za.png");

The constructor of this needs to point to a URI, so if you're pointing to something on the file system it'd be:

Image image = new Image("file:za.png");

Alternatively, you could do:

Image image = new Image(new File("za.png").toURI().toString());

...which is arguably neater. If the image is bundled in your jar rather than on the filesystem, you can obtain the URI like so:

Image image = new Image(getClass().getResource("za.jpg").toURI().toString());

Most methods / constructors in JavaFX that take a string as a parameter in this way (i.e. specifying a resource) do so via string URI's rather than just a plain file path or URL.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon