Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

javafx 2 - java.lang.IllegalArgumentException: Invalid URL or resource not found

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

        btnNo.setStyle("-fx-background-color:
"
                + "        #090a0c,
"
                + "        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),
"
                + "        linear-gradient(#20262b, #191d22),
"
                + "        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));
"
                + "    -fx-background-radius: 5,4,3,5;
"
                + "    -fx-background-insets: 0,1,2,0;
"
                + "    -fx-text-fill: white;
"
                + "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
"
                + "    -fx-font-family: "Arial";
"
                + "    -fx-text-fill: linear-gradient(white, #d0d0d0);
"
                + "    -fx-font-size: 12px;
"
                + "    -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);
"
                + "    -fx-border-width: 1 1 1 1;
"
                + "    -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;
"
                + "    -fx-font-size: 1.083333em;
"
                + "    -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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...