jQuery in Action, 3rd edition
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 Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] IllegalAccessException" Watch "[newbie] IllegalAccessException" New topic

[newbie] IllegalAccessException

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662

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

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):


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)

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

Joined: Oct 13, 2005
Posts: 44590
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: [newbie] IllegalAccessException
It's not a secret anymore!