Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Bizarre IllegalAccessError in servlet only.

843841Oct 23 2006 — edited Oct 24 2006
I am getting the following illegalAccessError when trying to access a perfectly legal field, but only when it's running as a webapp.
Here is the stackTrace:
java.lang.IllegalAccessError: tried to access field testb.testc.ParentClass.values from class testa.ChildClassB
testa.ChildClassB.toString(ChildClassB.java:22)
testd.NewClass.<init>(Unknown Source)
testserver.TestServlet.doGet(TestServlet.java:29)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Here is the code:
package testb.testc;

//Parent Class holds the static array "values"
public class ParentClass
{
protected static int[] values = {1, 2, 3, 4, 5, 6};
}

///////////////////////////////////
package testb.testc;

//First subclass, same package
public class ParentSubClassA extends ParentClass
{
}
///////////////////////////////////
package testb.testc;

//Second subclass, same package
public class ParentSubClassB extends ParentClass
{
}

/////////////////////////////////////
package testa;

import testb.testc.ParentSubClassA;
//subclass of first subclass, new package
public class ChildClassA extends ParentSubClassA
{

}

///////////////////////////////////////////
package testa;

import testb.testc.ParentSubClassB;
//subclass of second subclass, new package
//references super, super field values through subclass of first subclass
public class ChildClassB extends ParentSubClassB
{
public String toString()
{
   String s = "";
    for (int i = 0; i < ChildClassA.values.length; i++)
    {
        s += ChildClassA.values;
}
return s;
}
///////////////////////////////////////////////////
package testserver;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

import testa.ChildClassB;
//servlet calls toString on ChildClassB. Runs fine as "main". Throws IllegalAccessError
//when run as a servlet.
public class TestServlet extends HttpServlet
{
protected void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)
throws ServletException, IOException
{
PrintWriter out = httpServletResponse.getWriter();
ChildClassB newClass = new ChildClassB();
out.write("<html><body><p>");
out.write(newClass.toString());
out.write("</p></body></html>");

}

public static void main(String[] args)
{
ChildClassB nc = new ChildClassB();
System.out.println(nc.toString());
}
}



Does anyone know what's going on here?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2006
Added on Oct 23 2006
9 comments
210 views