Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

java.lang.IllegalAccessError

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
aman0711
Asked:
aman0711
  • 19
  • 11
  • 7
6 Solutions
 
CEHJCommented:
Can you post the complete error (stack trace)?
0
 
aman0711Author Commented:
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
 
CEHJCommented:
Could you post the Honey class?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
aman0711Author Commented:
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
 
aman0711Author Commented:
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
 
CEHJCommented:
Try commenting out the logging imports too
0
 
aman0711Author Commented:
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
 
CEHJCommented:
Try running a clean and rebuild on the whole project
0
 
aman0711Author Commented:
Hmmm.... ok...

Trying to run a simple sampe application... :-( will rebuild and run again
0
 
aman0711Author Commented:
wow.. this is frustrating. :(

still the same error
0
 
CEHJCommented:
I think that's the IBM jvm is it not? Try building it using a Sun one
0
 
aman0711Author Commented:
Yes thats IBM's jvm.

can i specify a different JVM in rad?
0
 
CEHJCommented:
It should be part of the project build config in Eclipse
0
 
aman0711Author Commented:
0
 
objectsCommented:
>   final static Logger logger = LoggerFactory.getLogger(TestExample.class);

change that to:

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

0
 
aman0711Author Commented:
Thanks object, will try it soon.. What do you think is the reason behind this?
0
 
objectsCommented:
incompatible jars. also check there aren't 2 versions of log4j in classpath

0
 
aman0711Author Commented:
o ok :-)
You are Java GOD :-)
0
 
CEHJCommented:
>>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
 
aman0711Author Commented:
Folks, that didnt work either :-(

The error I am getting is :

The method getLogger (class<TestExample>) is undefinedm for type logger.
0
 
aman0711Author Commented:
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
 
CEHJCommented:
Just use System.out debug statements (just a couple) while you're solving the logging problem
0
 
aman0711Author Commented:
Hi Charles, I tried putting Print statements, but nothing is getting printed on the console.

0
 
CEHJCommented:
Is this code inside a webapp?
0
 
aman0711Author Commented:
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
 
CEHJCommented:
I System.out doesn't work it sounds like the code isn't getting called. Make sure you have not ignored exceptions
0
 
objectsCommented:
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
 
aman0711Author Commented:
OK Objects :-)

BTW Happy Birthday... :-)
0
 
objectsCommented:
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
 
aman0711Author Commented:
Wish I could :-)
Aus little too far from me ...
Mick I saw you are a fan of Rugby... dont you follow cricket?
0
 
objectsCommented:
Yes enjoy watching cricket, rarely play these days. Mostly just backyard cricket.
0
 
aman0711Author Commented:
oh cool.. :-) Thats my game .....

I want to get in your Mentor progm soon... Need to clear SCJP
0
 
objectsCommented:
> 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
 
aman0711Author Commented:
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
 
objectsCommented:
yes that'll get to me

0
 
aman0711Author Commented:
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
 
CEHJCommented:
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 19
  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now