RFR: 8306707: Support pluggable image loading via javax.imageio

John Hendrikx jhendrikx at openjdk.org
Tue Oct 15 20:29:17 UTC 2024


On Tue, 15 Oct 2024 17:23:11 GMT, Kevin Rushforth <kcr at openjdk.org> wrote:

> With the restructuring to make the dependency optional at runtime, this seems like a reasonable approach to consider. I also want to make sure that we don't initialize the AWT toolkit unless an application tries to load an image file of a format that JavaFX doesn't support. It will take some time to review and test it.

I've used `ImageIO` with FX before, and it doesn't initialize AWT.  You can check with this code I think:


import java.lang.reflect.InvocationTargetException;
import java.net.URI;

import javax.imageio.ImageIO;

public class ImageIOAWTTest {
    public static void main(String[] args) throws Exception {
        // Check if AWT classes are loaded before using ImageIO
        boolean awtLoadedBefore = isClassLoaded("java.awt.Toolkit");

        // Load an image using ImageIO (this should not trigger AWT initialization)
        ImageIO.read(URI.create("https://picsum.photos/200/300").toURL());

        // Check if AWT classes are loaded after using ImageIO
        boolean awtLoadedAfter = isClassLoaded("java.awt.Toolkit");

        System.out.println("AWT loaded before ImageIO: " + awtLoadedBefore);
        System.out.println("AWT loaded after ImageIO: " + awtLoadedAfter);
    }

    // Method to check if a class is loaded by the ClassLoader
    private static boolean isClassLoaded(String className) throws NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException {
      java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[] { String.class });
      m.setAccessible(true);
      ClassLoader cl = ClassLoader.getSystemClassLoader();
      Object test1 = m.invoke(cl, className);
      return test1 != null;
    }
}


It will require `--add-opens=java.base/java.lang=ALL-UNNAMED` to run.

-------------

PR Comment: https://git.openjdk.org/jfx/pull/1593#issuecomment-2414941487


More information about the openjfx-dev mailing list