Java.Lang.Illegalaccesserror: Tried to Access Method

java.lang.IllegalAccessError: tried to access method

You are almost certainly using a different version of the class at runtime to the one you expect. In particular, the runtime class would be different to the one you've compiled against (else this would have caused a compile-time error) - has that method ever been private? Do you have old versions of the classes/jars on your system anywhere?

As the javadocs for IllegalAccessError state,

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.

I'd definitely look at your classpath and check whether it holds any surprises.

Two java files. Getting IllegalAccessError when running class with main method trying to access a method from the other file

Make sure each class is in the same folder, since the error is saying TapeDeckTestDrive can not find TapeDeck. I would recommend starting out with an IDE like Eclipse since it will help you focus more on coding and less with folder problems.

I know your code is all good (in java 8 at least) since when I copied it in eclipse it works no problem, meaning it has to be a folder problem, a problem with the installed version of java, or the way you are running the code is not working for some reason. If both files are in the exact same folder then I would make sure your java version says 1.8 something in the system files (Program Files(x86) most likely in windows), if it does not say that version then it could be another problem with the code and syntax for that version.
Another thing that might help is to put public behind the "class" on the first line of each class and make the Boolean public. This might be a syntax requirement on other versions of java or something that is needed when running off command prompt.

Java - Invoking a method causes IllegalAccessError

This is because you are not loading the dependency class first from the code. Put your MyOtherClass in its own file and set it as public. Then Load it as below.

 cl.loadClass("MyOtherClass");
cl.loadClass(class_name);

If you stick to have both the classes in the same file. Define it as below

public class MyClass {
class MyOtherClass {}
public void myMethod() {
MyOtherClass moc = new MyOtherClass();

}
}

java.lang.IllegalAccessError: tried to access class xyz from class zxy

Without the public modifier, the method is only available to the package the classes. See http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html. This applies to your exception class which is not public. The constructors and methods should also be public if you want global access.



Related Topics



Leave a reply



Submit