This discussion is archived
2 Replies Latest reply: Jun 23, 2011 3:22 AM by 870015 RSS

compiling a dynamically generated class and getting IllegalAccessError

870015 Newbie
Currently Being Moderated
Hi all.
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.

Thanks guyz.

Edited by: 867012 on Jun 20, 2011 5:45 AM
  • 1. Re: compiling a dynamically generated class and getting IllegalAccessError
    EJP Guru
    Currently Being Moderated
    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.
  • 2. Re: compiling a dynamically generated class and getting IllegalAccessError
    870015 Newbie
    Currently Being Moderated
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points