|
今天在使用JDK 9.0 环境下使用Hibernate 时候出现了这个错误,错误日志如下:
[java] java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
[java] at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
[java] at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:86)
[java] at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
[java] at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
故障原因:JAXB API是java EE 的API,因此在java SE 9.0 中不再包含这个 Jar 包。
java 9 中引入了模块的概念,默认情况下,Java SE中将不再包含java EE 的Jar包
而在 java 6/7 / 8 时关于这个API 都是捆绑在一起的
解决方案一:降低JDK 9 版本到 JDK 6/7/8
解决方案二:(亲测可行)手动加入这些依赖Jar包
要解决这个问题,我导入了下面这四个Jar包修复成功。
javax.activation-1.2.0.jar
http://search.maven.org/remotecontent?filepath=com/sun/activation/javax.activation/1.2.0/javax.activation-1.2.0.jar
jaxb-api-2.3.0.jar
http://search.maven.org/remotecontent?filepath=javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar
jaxb-core-2.3.0.jar
http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-core/2.3.0/jaxb-core-2.3.0.jar
jaxb-impl-2.3.0.jar
http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-2.3.0.jar
下载上面这些文件和复制他们到libs文件夹下,
添加他们导入到Build Path中
重新运行即可
解决方案三:Maven项目可添加如下依赖:
<!-- Java 6 = JAX-B Version 2.0 -->
<!-- Java 7 = JAX-B Version 2.2.3 -->
<!-- Java 8 = JAX-B Version 2.2.8 -->
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies> |
Tips:
建议使用中心仓库,否则可能某些jar找不到:
HTTP: http://repo1.maven.org/maven2
HTTPS:https://repo1.maven.org/maven2
测试代码:
测试类:
public class DatabaseConnectionTools {
private static final SessionFactory ourSessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
System.out.println("querying all the managed entities...");
final Metamodel metamodel = session.getSessionFactory().getMetamodel();
for (EntityType<?> entityType : metamodel.getEntities()) {
final String entityName = entityType.getName();
final Query query = session.createQuery("from " + entityName);
System.out.println("executing: " + query.getQueryString());
for (Object o : query.list()) {
System.out.println(" " + o);
}
}
} finally {
session.close();
}
}
} |
---------------------
原文:https://blog.csdn.net/hadues/article/details/79188793
程序猿的技术大观园:www.javathinker.net
[这个贴子最后由 flybird 在 2020-02-24 10:26:53 重新编辑]
|
|