Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8320164

JavaFX Image cannot load windows file paths anymore

XMLWordPrintable

    • x86_64
    • windows

      ADDITIONAL SYSTEM INFORMATION :
      Windows and Java 11+

      A DESCRIPTION OF THE PROBLEM :
      With JavaFX 21 and newer, the Image constructor throws an IllegalArgumentException for file paths on windows that can be resolved by java.io.File, i.e. a path starting with a drive letter like `C:\` was accepted but is not anymore.

      Starting with the jfx21 branch, the lines [1130-1132](https://github.com/openjdk/jfx/blob/jfx20/modules/javafx.graphics/src/main/java/javafx/scene/image/Image.java#L1130-L1132) that accepted existing files are missing.

      REGRESSION : Last worked in version 20

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Calling the Image constructor with a valid and existing path to an image file should work as in previous versions:

      String pathToAnExistingAndValidImageFile = "C:\\Data\\test.png";
      new Image(pathToAnExistingAndValidImageFile);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The image should be created without throwing an exception.
      ACTUAL -
      java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c

      ---------- BEGIN SOURCE ----------
      private Image getTestImage() throws FileNotFoundException {
          String pathToAnExistingAndValidImageFile = "C:\\Data\\test.png";
          File existingImageFile = new File(pathToAnExistingAndValidImageFile);
          if (existingImageFile.exists()) {
              return new Image(pathToAnExistingAndValidImageFile);
          } else {
              throw new FileNotFoundException(pathToAnExistingAndValidImageFile);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      We need to get a now valid url description with either `URI#toString(): String` or `URL#toExternalForm(): String`.

      The example code above would work like this:
      ```
      private Image getTestImage() {
          String pathToAnExistingAndValidImageFile = "C:\\Data\\test.png";
          File existingImageFile = new File(pathToAnExistingAndValidImageFile);
          return new Image(existingImageFile.toURI().toString());
      }
      ```

      FREQUENCY : always


        1. ImageView.java
          1 kB
          Praveen Narayanaswamy
        2. java-coffee-cup-logo.png
          4 kB
          Praveen Narayanaswamy

            kcr Kevin Rushforth
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: