Unable to make field private final java.util.comparator java.util.treemap.comparator accessible: module java.base does not “opens java.util” to unnamed module

When you encounter the error message “unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not ‘opens java.util’ to unnamed module”, it means that you are trying to access a private final field in the java.util.TreeMap class called comparator, but it is not accessible because the java.base module does not open the java.util package to the unnamed module.

This error typically occurs when you are using a newer version of Java (9 or above) that enforces module encapsulation. In these versions, modules control the accessibility of their packages to other modules, and if a package is not opened to a specific module, it cannot access the contained classes or fields.

To resolve this issue, you need to explicitly open the java.util package to the unnamed module. Here’s an example of how you can do it:

  
  module your.module.name {
    // Open the java.util package to the unnamed module
    opens java.util;
  }
  
  

In the module-info.java file of your module, you need to add the “opens” directive followed by the package name you want to make accessible. In this case, you want to make the java.util package accessible.

After making this change, the private final field java.util.Comparator in java.util.TreeMap.comparator will be accessible to your module, and the error should be resolved.

Read more