Illegal reflective access operation warning with iText PDF FontFactory.registerDirectories() and Java 11

The only clean solution is to switch to a newer version that does not perform this illegal reflective access, once this new version exists.

As a temporary work-around, you could use

package com.example;

import org.junit.Test;

import com.itextpdf.text.FontFactory;
import org.junit.BeforeClass;

public class FontFactoryTest {
    @BeforeClass
    public static void prepare() {
        Module mod = FontFactory.class.getClassLoader().getUnnamedModule();
        if(mod == FontFactory.class.getModule()) {
            Object.class.getModule().addOpens("java.nio", mod);
        }
    }

    @Test
    public void test() {
            FontFactory.registerDirectories();
    }
}

This only work as long as the code is placed in the unnamed module (loaded via class path, rather than module path) and as long as the JDK grants the reflective access by default (this is expected to change in the future).

Otherwise, you would need an -add-opens option at the command line or something similar (assuming that the particular execution environment has such an option) and it still is not guaranteed to work, as it implies an access to a member that doesn’t need to be there at all in a particular implementation.

A module can not add an “opens” edge if it doesn’t have the permission itself. The reason, it works at all, is that the “opens” edge from the java.base module to the unnamed module does already exist, it’s just augmented with this warning. The code above utilizes this permission to add an explicit “opens” edge, which is not linked with the warning. As said, this is only a temporary work-around that will stop working in future versions.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top