I'm working on a project where i have to compile codes generated on the fly. My problem is this: The dynamically generated codes are given a package, say x.y.z. Now, this package actually exists in the project and has a couple of other classes in it, among which is an interface (say I_Special) that the dynamic classes implement. This interface is declared "package private". After dynamic compilation, i use a customized class loader to load the compiled class, but this is where my headache lies as it throws
"java.lang.IllegalAccessError: class x.y.z.DynamicClass__1 cannot access its superinterface x.y.z.I_Special"
I have tried copying the generated code and putting it into the project. When i compile it this way and run, it runs smoothly. So i suspect that the problem is from my class loader, though i may be wrong.
Please i'll appreciate any input to point me in the right direction to solve this.
Edited by: 867012 on Jun 20, 2011 5:45 AM
From the Javadoc for IllegalAccessException:
'An IllegalAccessException is thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.'
The customized class loader needs to have access to that interface, typically by using the current context class loader as its parent.
Thanks a lot for that. I figured, after some fiddling, that the files within the package were being loaded by the current context class loader, and as such, were only publicly accessible to my dynamically generated classes which were being loaded by my custom class loader. All i had to do was make the methods and interfaces i needed public-though this broke some of my design models (i had a neat work around for this though).
Thanks once again.