java.lang.IllegalAccessException upon reflective class instantiation

This evening I got the following exception when reflectively instantiating a class.

Caused by: java.lang.IllegalAccessException: Class foo.Baz can not access a member of class foo.Bar with modifiers ""
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65) [na:1.6.0_16]
	at java.lang.Class.newInstance0(Class.java:349) [na:1.6.0_16]
	at java.lang.Class.newInstance(Class.java:308) [na:1.6.0_16]
	at foo.bar(Baz.java:126) [build/:na]
	... 90 common frames omitted

If you get this all you need to do is to make sure that the constructor of the class you are trying to instantiate is visible to the class that is doing the reflective instantiation. The empty modifier string in the exception message is confusing at first but simply means that your constructor is package private and therefore has no access modifier. If your constructor has an access modifier it will show within that string.

Credits: Troubleshooting the reflective API.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s