I migrated older printer API code to Java 8, and the following warning appears:
Access restriction: The method 'AppContext.getAppContext()' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_51\lib\rt.jar')
It is due to this code (originally sourced from this SO question):
/**
* Printer list does not refresh itself; need to run this to refresh if necessary.
*/
public static void refreshSystemPrinterList() {
Class[] classes = PrintServiceLookup.class.getDeclaredClasses();
for (int i = 0; i < classes.length; i++) {
if ("javax.print.PrintServiceLookup$Services".equals(classes[i].getName())) {
AppContext.getAppContext().remove(classes[i]); // the line that throws the warning
break;
}
}
As I understand it, PrintServiceLookup
loads a list of visible printers when it is classloaded, but it apparently does not refresh this list, or have the ability trigger a refresh. The way for a long-running application to refresh the list would be to either unload the class, which is what the code is doing, or restart itself.
Mild research on the warning reveals come Java 9, AppContext.getAppContext()
will be inaccessible. Without further research, my current remediation idea is to use an accessible classloader for loading this class, and purging the classloader when this method is called.
Ultimately, I want to know what I need to do to properly replace this. Currently running on Windows, may be moving to Linux ; I see a lot of updates to this SO question regarding the Linux implementation.