javaillegalaccessexception

How to access `AWTAccessor.getCursorAccessor().setPData()`


I'm trying to run AWTAccessor.getCursorAccessor().setPData(this, cursor); for a cursor related operation for the class sun.awt.AWTAccessor.

I'm getting the error:

Exception in thread "main" java.lang.IllegalAccessError: class xawtcursor.XImageCursor (in unnamed module @0x3d3fcdb0) cannot access class sun.awt.AWTAccessor (in module java.desktop) because module java.desktop does not export sun.awt to unnamed module @0x3d3fcdb0
    at xawtcursor.XImageCursor.createNativeCursor(XImageCursor.java:25)
    ...

Some test code (the 1 is invalid here, but it's just to simplify the test code a bit so it can be reproduced):

package xawtcursor;

import sun.awt.AWTAccessor;
import sun.awt.X11.XCustomCursor;
import sun.awt.X11.XToolkit;

import java.awt.*;

public class XImageCursor extends XCustomCursor {
    public XImageCursor(Image cursor, Point hotSpot, String name) throws IndexOutOfBoundsException {
        super(cursor, hotSpot, name);
    }

    protected void createNativeCursor(Image im, int[] pixels, int width, int height, int xHotSpot, int yHotSpot) {
        XToolkit.awtLock();
        try {
            AWTAccessor.getCursorAccessor().setPData(this, 1);
        } finally {
            XToolkit.awtUnlock();
        }
    }
}

I tried --add-exports=java.desktop/sun.awt.X11=ALL-UNNAMED which fixed one error. What's the compilation option to let me access this one?


Solution

  • Turns out I just need to add multiple --add-exports options. Adding the following works:

    --add-exports=java.desktop/sun.awt=ALL-UNNAMED --add-exports=java.desktop/sun.awt.X11=ALL-UNNAMED