Wednesday, October 29, 2008

solving java.lang.IllegalAccessError exception

Java documentation says IllegalAccessError is usally thrown if an application attempts to access or modify a field, or to call a method that it does not have access to. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
The exception format will be "java.lang.IllegalAccessError:tried to access class CLASS2 from class CLASS1".Compile time exceptions will easy surface, so now we will talk about the runtime exceptions.

I will explain the scenario I faced and you can relate it your problem.
My application CLASS1 is compiled using JDK1.3 and CLASS2 from Oracle JDBC driver.During compilation I have been using a certain version of JDBC driver.
Now I had to test my application with a newer version of the JDBC Driver[CLASS2].Note that I compiled my CLASS1 with older JDBC driver in place.Now When I tried to use the newer version of CLASS2,the above exception is thrown.
Solution:
CLASS2 in the older version of driver was PUBLIC and in the later version it is made private.So when I tried to access the method at runtime the IllegalAccessError exception is thrown.
In general check if the class or a method or a member variable's property has been changed from PUBLIC to private/protected.This is a good point to start troubleshooting this sort of issues.
It will also be better if you re-compile the CLASS1 with problematic CLASS2 in place.



No comments: