我正在尝试执行简单的hibernate java程序 -
使用的文件: hibernate.cfg.xml,Employees.cfg.xml
Java类: HibernateUtil.java,Employees.java,Gender.java(Enum),InsertIntoEmployee.java
InsertIntoEmployee.java
的代码package com.action.utilities.businessutilities;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.action.utilities.hibernateutilities.HibernateUtil;
import com.hibernate.models.Employees;
import com.hibernate.models.Gender;
public class InsertIntoEmployee {
public InsertIntoEmployee() {
// TODO Auto-generated constructor stub
}
/**
* @param args
* @throws ClassNotFoundException
* @throws ParseException
*/
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
Class.forName("com.action.utilities.hibernateutilities.HibernateUtil");
Session session=HibernateUtil.getSessionFactory().openSession();
try {
Transaction tx=session.beginTransaction();
Employees emp=new Employees();
emp.setFirstName("Gaurav");
emp.setLastName("Palkar");
emp.setBirthDate(sdf.parse("01/17/1983"));
emp.setGender(Gender.MALE);
session.save(emp);
session.getTransaction().commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(ParseException pe){
pe.printStackTrace();
}catch(Exception ee){
ee.printStackTrace();
}finally{
session.close();
}
}
}
收到以下错误:
java.lang.NoClassDefFoundError: javax/transaction/SystemException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2248)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:176)
at com.action.utilities.hibernateutilities.HibernateUtil.<clinit>(HibernateUtil.java:30)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.action.utilities.businessutilities.InsertIntoEmployee.main(InsertIntoEmployee.java:29)
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 9 more
Exception in thread "main" java.lang.NullPointerException
at com.action.utilities.businessutilities.InsertIntoEmployee.main(InsertIntoEmployee.java:30)
Classpath条目全部如下:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry excluding="src/" kind="src" path=""/>
<classpathentry kind="lib" path="lib/jta.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.18-bin.jar"/>
<classpathentry kind="lib" path="lib/asm-attrs-1.5.3.jar"/>
<classpathentry kind="lib" path="lib/c3p0-0.9.1.jar"/>
<classpathentry kind="lib" path="lib/ehcache-1.2.3.jar"/>
<classpathentry kind="lib" path="lib/ejb3-persistence-1.0.2.GA.jar"/>
<classpathentry kind="lib" path="lib/jdbc-stdext-2.0.jar"/>
<classpathentry kind="lib" path="lib/antlr-2.7.7.jar"/>
<classpathentry kind="lib" path="lib/avro-1.5.1.jar"/>
<classpathentry kind="lib" path="lib/hibernate-commons-annotations-4.0.1.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-core-4.1.3.Final.jar"/>
<classpathentry kind="lib" path="lib/jackson-core-asl-1.9.2.jar"/>
<classpathentry kind="lib" path="lib/jackson-mapper-asl-1.9.2.jar"/>
<classpathentry kind="lib" path="lib/javassist-3.15.0-GA.jar"/>
<classpathentry kind="lib" path="lib/jboss-logging-3.1.0.GA.jar"/>
<classpathentry kind="lib" path="lib/lucene-core-3.5.0.jar"/>
<classpathentry kind="lib" path="lib/paranamer-2.3.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.6.1.jar"/>
<classpathentry kind="lib" path="lib/snappy-java-1.0.1-rc3.jar"/>
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="lib/asm-3.1.jar"/>
<classpathentry kind="lib" path="lib/cglib-2.1_3.jar"/>
<classpathentry kind="lib" path="lib/commons-collections-3.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.13.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
过去三天我一直坚持这个问题。
谷歌搜索后我才知道是否应该使用 javaee-api-5.jar 来克服问题,但它没有帮助。我在下面附上了classformaterror -
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract
in class file javax/transaction/SystemException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2248)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:176)
at com.action.utilities.hibernateutilities.HibernateUtil.<clinit>(HibernateUtil.java:30)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.action.utilities.businessutilities.InsertIntoEmployee.main(InsertIntoEmployee.java:29)
Exception in thread "main" java.lang.NullPointerException
at com.action.utilities.businessutilities.InsertIntoEmployee.main(InsertIntoEmployee.java:30)
然后我在类路径中尝试了 glassfish-api-4.0-b77.jar ,然后它也没有帮助。
任何机构都可以建议克服这个例外......
答案 0 :(得分:1)
尝试添加javax.transaction.jar
。这包含javax/transaction/SystemException