Solved

java.lang.IllegalAccessError

Posted on 2009-05-07
37
650 Views
Last Modified: 2013-11-24
Hi experts,
                 I am trying to run a simple hibernate sample application. I am getting the following error on running the code.

     The java class could not be loaded. java.lang.IllegalAccessError

 Can someone help me out with it
package de.laliluna.example;
 
import java.util.Iterator;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import de.laliluna.hibernate.SessionFactoryUtil;
 
public class TestExample {
 
  final static Logger logger = LoggerFactory.getLogger(TestExample.class);
 
  /**
   * @param args
   */
  public static void main(String[] args) {
    Honey forestHoney = new Honey();
   forestHoney.setFirstName("SomeName");
   forestHoney.setLastName("AnyName");
    
    Honey countryHoney = new Honey();
   countryHoney.setFirstName("CountryName");
   countryHoney.setLastName("AnyCountryName");
    
    createHoney(forestHoney);
    createHoney(countryHoney);
    // our instances have a primary key now:
    logger.debug("{}", forestHoney);
    logger.debug("{}", countryHoney);
    listHoney();
    deleteHoney(countryHoney);
    listHoney();
    forestHoney.setFirstName("any name");
    updateHoney(forestHoney);
 
  }
 
  private static void listHoney() {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      List honeys = session.createQuery("select h from Honey as h")
              .list();
      for (Iterator iter = honeys.iterator(); iter.hasNext();) {
        Honey element = (Honey) iter.next();
        logger.debug("{}", element);
      }
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
 
 
    }
  }
 
  private static void deleteHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.delete(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
 
  private static void createHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.save(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
 
   private static void updateHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.update(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
}

Open in new window

0
Comment
Question by:aman0711
  • 19
  • 11
  • 7
37 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24329570
Can you post the complete error (stack trace)?
0
 
LVL 10

Author Comment

by:aman0711
ID: 24329761
Nothing Charles, I am only getting this in the console :-(

The java class could not be loaded. java.lang.IllegalAccessError

I have attached folder structure as well

laliluna.PNG
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24329831
Could you post the Honey class?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Author Comment

by:aman0711
ID: 24329841
here it is


package de.laliluna.example;
 
public class Honey {
 
      private String firstName;
      private String lastName;
      private String email;
 
      
      public Honey() {
    	  
      }
      
   	public String getEmail() {
		return email;
	}
 
 
 
	public void setEmail(String email) {
		this.email = email;
	}
 
 
 
 
 
	public String getFirstName() {
		return firstName;
	}
 
 
 
 
 
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
 
 
 
 
 
	public String getLastName() {
		return lastName;
	}
 
 
 
 
 
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
 
 
 
 
 
	public String toString() {
		return "Honey: "+getEmail()+" Name: "+getFirstName()+" Taste: "+getLastName();
	}
	
	
}

Open in new window

0
 
LVL 10

Author Comment

by:aman0711
ID: 24330597
If i comment out all the logger statements in the TestExample.java then this is what I am getting


IWAV0055I Java Bean de.laliluna.example.TestExample started with the main method
Inside main method
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:618)
	at org.eclipse.ve.internal.java.vce.launcher.remotevm.JavaBeansLauncher.main(JavaBeansLauncher.java:79)
Caused by: java.lang.IllegalAccessError
	at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
	at java.lang.J9VMInternals.initializeImpl(Native Method)
	at java.lang.J9VMInternals.initialize(J9VMInternals.java:194)
	at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:151)
	at java.lang.J9VMInternals.initializeImpl(Native Method)
	at java.lang.J9VMInternals.initialize(J9VMInternals.java:194)
	at de.laliluna.hibernate.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:35)
	at java.lang.J9VMInternals.initializeImpl(Native Method)
	at java.lang.J9VMInternals.initialize(J9VMInternals.java:194)
	at de.laliluna.example.TestExample.createHoney(TestExample.java:96)
	at de.laliluna.example.TestExample.main(TestExample.java:31)
	... 5 more
IWAV0052E Invocation Target Exception creating de.laliluna.example.TestExample

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 24330646
Try commenting out the logging imports too
0
 
LVL 10

Author Comment

by:aman0711
ID: 24330678
Commented out everything, below is the console output:

Inside main method
Exception in thread "main" java.lang.IllegalAccessError
    at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:194)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:151)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:194)
    at de.laliluna.hibernate.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:35)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:194)
    at de.laliluna.example.TestExample.createHoney(TestExample.java:94)
    at de.laliluna.example.TestExample.main(TestExample.java:29)

package de.laliluna.example;
 
import java.util.Iterator;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
 
import de.laliluna.hibernate.SessionFactoryUtil;
 
public class TestExample {
 
//  final static Logger logger = LoggerFactory.getLogger(TestExample.class);
  
 
 
 
  public static void main(String[] args) {
	  System.out.println("Inside main method");
    Honey forestHoney = new Honey();
    forestHoney.setName("forest honey");
    forestHoney.setTaste("very sweet");
    Honey countryHoney = new Honey();
    countryHoney.setName("country honey");
    countryHoney.setTaste("tasty");
    createHoney(forestHoney);
    createHoney(countryHoney);
    // our instances have a primary key now:
//    logger.debug("{}", forestHoney);
//    logger.debug("{}", countryHoney);
    listHoney();
    deleteHoney(countryHoney);
    listHoney();
    forestHoney.setName("Norther Forest Honey");
    updateHoney(forestHoney);
 
  }
 
  private static void listHoney() {
	  System.out.println("Inside listHoney method");
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      List honeys = session.createQuery("select h from Honey as h")
              .list();
      for (Iterator iter = honeys.iterator(); iter.hasNext();) {
        Honey element = (Honey) iter.next();
//       logger.debug("{}", element);
      }
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
//          logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
 
 
    }
  }
 
  private static void deleteHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.delete(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
 //         logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
 
  private static void createHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.save(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
  //        logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
 
   private static void updateHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.update(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
  //        logger.debug("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
}

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 24330848
Try running a clean and rebuild on the whole project
0
 
LVL 10

Author Comment

by:aman0711
ID: 24330864
Hmmm.... ok...

Trying to run a simple sampe application... :-( will rebuild and run again
0
 
LVL 10

Author Comment

by:aman0711
ID: 24330919
wow.. this is frustrating. :(

still the same error
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24330964
I think that's the IBM jvm is it not? Try building it using a Sun one
0
 
LVL 10

Author Comment

by:aman0711
ID: 24330987
Yes thats IBM's jvm.

can i specify a different JVM in rad?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 24331123
It should be part of the project build config in Eclipse
0
 
LVL 10

Author Comment

by:aman0711
ID: 24331140
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24331888
>   final static Logger logger = LoggerFactory.getLogger(TestExample.class);

change that to:

  final static Logger logger = Logger.getLogger(TestExample.class);

0
 
LVL 10

Author Comment

by:aman0711
ID: 24331919
Thanks object, will try it soon.. What do you think is the reason behind this?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24331932
incompatible jars. also check there aren't 2 versions of log4j in classpath

0
 
LVL 10

Author Comment

by:aman0711
ID: 24331944
o ok :-)
You are Java GOD :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24334370
>>Thanks object, will try it soon.

No need. According to your earlier comment, you've commented out all logging code in what you posted and you tell us you still get errors. Just to be 100% sure, can you confirm that, and post the latest errors
0
 
LVL 10

Author Comment

by:aman0711
ID: 24336063
Folks, that didnt work either :-(

The error I am getting is :

The method getLogger (class<TestExample>) is undefinedm for type logger.
0
 
LVL 10

Author Comment

by:aman0711
ID: 24336127
Could you please tell me a code that would test Honey class using hibernate without the logger?

/**
 * Example class
 * @author Sebastian Hennebrueder
 * created Jan 16, 2006
 * copyright 2006 by http://www.laliluna.de
 */
package de.laliluna.example;
 
public class Honey {
 
	private Integer id;
 
	private String name;
 
	private String taste;
	
	public Honey() {
 
	}
	
 
	public Integer getId() {
		return id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String getTaste() {
		return taste;
	}
 
	public void setTaste(String taste) {
		this.taste = taste;
	}
	
	public String toString() {
		return "Honey: "+getId()+" Name: "+getName()+" Taste: "+getTaste();
	}
	
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24336142
Just use System.out debug statements (just a couple) while you're solving the logging problem
0
 
LVL 10

Author Comment

by:aman0711
ID: 24336191
Hi Charles, I tried putting Print statements, but nothing is getting printed on the console.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24336494
Is this code inside a webapp?
0
 
LVL 10

Author Comment

by:aman0711
ID: 24336568
Hi Charles,
                     This is just a plain java project. I have attached the snap shot of folder structure.

The tutorial is located at:

http://www.laliluna.de/first-hibernate-example-tutorial.htmlhttp://www.laliluna.de/first-hibernate-example-tutorial.html
laliluna.PNG
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24336605
I System.out doesn't work it sounds like the code isn't getting called. Make sure you have not ignored exceptions
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24342044
theres nothing worong with your code

As I mentioned earlier check your logging jars, you have incompatible versions
I would suggest upgrading all the slf4j jars to the latest version

0
 
LVL 10

Author Comment

by:aman0711
ID: 24342062
OK Objects :-)

BTW Happy Birthday... :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 24342078
thanks mate, we're having a double celebratioon as Objects (the company) had its birthday on Thursday
you should drop around for a beer :)

http://www.objects.com.au/news.html
0
 
LVL 10

Author Comment

by:aman0711
ID: 24342087
Wish I could :-)
Aus little too far from me ...
Mick I saw you are a fan of Rugby... dont you follow cricket?
0
 
LVL 92

Expert Comment

by:objects
ID: 24342115
Yes enjoy watching cricket, rarely play these days. Mostly just backyard cricket.
0
 
LVL 10

Author Comment

by:aman0711
ID: 24342123
oh cool.. :-) Thats my game .....

I want to get in your Mentor progm soon... Need to clear SCJP
0
 
LVL 92

Expert Comment

by:objects
ID: 24342164
> I want to get in your Mentor progm soon

great, we have some spots opening up in the next couple of weeks

> Need to clear SCJP

overrated imo ;) drop me a message if you want to hear my thoughts on the subject

0
 
LVL 10

Author Comment

by:aman0711
ID: 24342182
Hmmm... anytime Mick.

does dropping a message on objects.com goes to you? or its just for feedback..

really need your suggestions on all this.
0
 
LVL 92

Expert Comment

by:objects
ID: 24342195
yes that'll get to me

0
 
LVL 10

Author Comment

by:aman0711
ID: 24342203
Cool.. then I will surely drop you a message there tomorrow to have your thoughts on SCJP .... Dont wanna bug you on your special day :)

again many many happy returns of the day...

Have fun, Drink beer :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24398800
:-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question