简体   繁体   中英

Adding a class to the Swing package — result is an IllegalAccessError on a visible function

I am creating a new Widget using the same structure/style as the other Swing widgets. To this end I have put my UI class in javax.swing.plaf.basic. As part of my UI implementation I am using the javax.swing.plaf.basic.LazyActionMap class. Eclipse is considering it to be visible as it has the same package name, however at runtime I get a java.lang.IllegalAccessError exception (presumably it is not visible at runtime).

It seem that even though I have given my class the same package name, and Eclipse is considering them to be in the same package, at runtime they are not.

Is there something I am doing incorrectly? The reason I am trying to have visibility to internal library classes is because for the prototype of the widget I am re-using the JSlider UI code. In the future I will transition to drawing the slider from scratch.

Classes loaded through a different class loader are considered to belong to different packages. At compile time your compiler will not know how you are going to deploy, so can't see the mistake. You could add the code as system classes with -Xbootclasspath/a: , but that's very naughty and you shouldn't do it.

You should put your own classes in your own packages, well outside of javax . java.* classes wont even get loaded by non-bootstrap class loaders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM