• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How To Resolve java.lang.IllegalAccessException

April 24, 2014 //  by Krishna Srinivasan

This example shows when the java.lang.IllegalAccessException is thrown in your application. When an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, if that method is not accessible to your application (probably the modifier is private or not accessible one), then your application will throw java.lang.IllegalAccessException. This exception is extended from java.lang.ReflectiveOperationException.

Lets look at this simple example which throws java.lang. IllegalAccessException.

ClassNewInstanceExample.java

package javabeat.net.lang;

/**
 * java.lang.IllegalAccessException Example
 *
 * @author Krishna
 *
 */
public class ClassNewInstanceExample {
	public static void main(String[] args) throws IllegalAccessException,
			InstantiationException {
		Class<?> classVar = ExampleClass.class;
		ExampleClass t = (ExampleClass) classVar.newInstance();
		t.testMethod();
	}

}

ExampleClass.java

package javabeat.net.lang;

public class ExampleClass {
	private ExampleClass(){

	}
	public void testMethod(){
		System.out.println("Method 'testMethod' Called");
	}
}

Exception Trace…

If you run the above program, you will get the following exception trace as the output.

Exception in thread "main" java.lang.IllegalAccessException: Class javabeat.net.lang.ClassNewInstanceExample can not access a member of class infosys.net.lang.ExampleClass with modifiers "private"
	at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at javabeat.net.lang.ClassNewInstanceExample.main(ClassNewInstanceExample.java:13)

The solution for this problem is to check the method or constructor whether it has the proper access modifier to access in your application.

Category: JavaTag: Java Exceptions, Java Lang

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: «java How To Compress Image Using Java
Next Post: How To Open Word Document In Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact