Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes [newbie] IllegalAccessException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] IllegalAccessException" Watch "[newbie] IllegalAccessException" New topic
Author

[newbie] IllegalAccessException

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

I am trying to create a new instance of a class within the package:



Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type ClassNotFoundException
Unhandled exception type InstantiationException
Unhandled exception type IllegalAccessException

at homenetwork.bkr.training.ReflectionTest.main(ReflectionTest.java:27)

I also tried to use it within another function (where actually I want it):

HelperClassesforClasses.java




Output:
java.lang.ClassNotFoundException: homenetwork.bkr.training
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at homenetwork.bkr.training.ReflectionTest.main(ReflectionTest.java:28)



Jon
Abhijeet Ravankar
Ranch Hand

Joined: Mar 15, 2009
Posts: 62
class.forName() throws ClassNotFoundException and public newInstance() throws InstantiationException and IllegalAccessException. So, I hope your entire code is in try-catch block.

I also had problems sometimes with Class.forName(String) function. Instead of that, try to use the other variant:

public static Class<?> forName(String name,boolean initialize,ClassLoader loader) throws ClassNotFoundException



Hope it helps.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Afraid your suggestion of Class#forName in the catch won't work, because you are throwing an un-handled checked Exception.

I recommend a trip to the Java™ Tutorials.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [newbie] IllegalAccessException