Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

spring - Hibernate, Java 9 and SystemException

I've been trying to run Hibernate 5.2.11 application in Java 9/Spring Boot 1.5.x/Maven project but I'm failing at missing class:

Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)
    at org.jboss.logging.Logger$1.run(Logger.java:2554)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
    at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
    at org.hibernate.internal.HEMLogging.messageLogger(HEMLogging.java:28)
    at org.hibernate.internal.HEMLogging.messageLogger(HEMLogging.java:24)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<clinit>(EntityManagerFactoryBuilderImpl.java:115)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 33 more

Has anyone encountered this exception and knows a workaround? I tried adding --add-modules for javax.bind or java.se.ee but they didn't help.

The above error shows in a mavan-failsafe (2.20.1) integration test that starts Spring context with Hibernate. Application doesn't have any Java 9 specific code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the migration guide and the java docs, since the module java.transaction which exports the package javax.transaction has been marked as @Deprecated.

You should ideally migrate your code to be using javaee/javax.transaction instead. Currently, you can do so using automatic module converted from the dependency:

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>javax.transaction-api</artifactId>
    <version>1.2</version>
</dependency>

and adding to the module-info.java the following:-

requires javax.transaction.api;

Additionally while using the maven-failsafe-plugin, make sure you are using the minimum compatible version 2.20.1 or above as mentioned in the progress document of Maven.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.20.1</version>
</plugin>

@Deprecated(forRemoval="after OP's confirmation")

On the other hand, a temporary workaround (since eventually these modules will be removed from the JDK) could be to make use of:-

--add-modules java.transaction

As mentioned in the comments, since the required dependency for javax.transaction-api is already available on the classpath, you shouldn't be required to add any compiler option or else you would end up overriding the current package with the java.transaction module's exported javax.transaction package which ideally for your use case doesn't consist of SystemException.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...