0

I have a file named dog.jpg that resides in c:\Temp. So its full Windows path is C:\Temp\dog.jpg

Every answer on this site suggests to replace the \ with a /, but none of these statements I tried seems to work:

Image image = new Image("C:\\Temp\\dog.jpg");
Image image = new Image("C://Temp//dog.jpg");
Image image = new Image("C:/Temp/dog.jpg");

(btw, it does work if I put dog.jpg in the current working directory and use:

Image image = new Image("dog.jpg");

)

I am getting the following exception report:

Exception in Application start method
Exception in thread "main" 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 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: unknown protocol: c
        at javafx.scene.image.Image.validateUrl(Image.java:1121)
        at javafx.scene.image.Image.<init>(Image.java:620)
        at Inclass_week7_session_1_4.start(Inclass_week7_session_1_4.java:21)
        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.net.MalformedURLException: unknown protocol: c
        at java.net.URL.<init>(Unknown Source)
        at java.net.URL.<init>(Unknown Source)
        at java.net.URL.<init>(Unknown Source)
        at javafx.scene.image.Image.validateUrl(Image.java:1115)




C:\Java\Tutorial\JavaFX 2>javac -version
javac 1.8.0_102

What am I doing wrong? And how can I get this Windows absolute path to work?

Thanks for your help.

naswari
  • 1
  • 1
  • I always use Java's File class to see if the file exists. Also, where is your ImageView. I also go to the folder that the file is located in and copy the file explorer's address. Then I add the file name and extension. – SedJ601 Jul 06 '17 at 21:35
  • 1
    Possible duplicate of [Cannot load image in JavaFX](https://stackoverflow.com/questions/16099427/cannot-load-image-in-javafx) – Joe C Jul 06 '17 at 21:35
  • Look at the Docs. The `Image(String)`-Constructor takes a URL-String! You can also see that in your Stacktrace: `Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c`. You have to use the `file` Protocol (see Joe C's comment) or open a `FileInputStream` and use the Constructor `Image(InputStream)` – Felix Jul 06 '17 at 21:45

1 Answers1

1

Image class constructor requires a url rather than an absolute path, so add the protocol in your url

 Image img = new Image("file:///C:/Temp/dog.jpg");