jar包含javax.transaction.SystemException却报ClassNotFoundException问题
javax.transaction.SystemException ClassNotFoundException (Even When the Class Exists in the Jar) Let's break down your problem and walk through practical solutions—first off, the directory structure of your class (javax/transaction/SystemException.class) is correct; it matches standard Java package naming conventions, so that's not the root issue here.
Your Problem Context
You're encountering this error:
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
Even though the class exists in your jar at the proper path. Here's your full error log for reference:
:: Spring Boot :: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) at com.drbanner.bidder.MainClass.main(MainClass.java:56) Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException at java.lang.Class.forName0(java.base@9-internal/Native Method) at java.lang.Class.forName(java.base@9-internal/Class.java:378) at org.jboss.logging.Logger$1.run(Logger.java:2554) at java.security.AccessController.doPrivileged(java.base@9-internal/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:116) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:51) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:388) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ... 13 more Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException at jdk.internal.loader.BuiltinClassLoader.loadClass(java.base@9-internal/BuiltinClassLoader.java:366) at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(java.base@9-internal/ClassLoaders.java:184) at java.lang.ClassLoader.loadClass(java.base@9-internal/ClassLoader.java:419) ... 29 more
And your partial pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.drbanner</groupId> <artifactId>bidder</artifactId> <version>0.0.1-SNAPSHOT</version> <name>bidder </name> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId>
Possible Causes & Fixes
1. JDK 9+ Module System Conflict
From your log, you're using JDK 9 (java.base@9-internal). Starting with JDK 9, Java EE modules like javax.transaction were moved to optional modules that aren't loaded by default. This is a common pain point when pairing older Spring Boot/Hibernate versions with JDK 9+.
Fixes:
- Downgrade to JDK 8: This is the quickest solution—JDK 8 includes the full
javax.transactionpackage without module restrictions, so it avoids this issue entirely. - Add Module Flags: If you need to stick with JDK 9+, add this JVM argument when launching your app:
Or configure it directly in your Maven compiler plugin:--add-modules java.transaction<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>9</release> <compilerArgs> <arg>--add-modules</arg> <arg>java.transaction</arg> </compilerArgs> </configuration> </plugin>
2. Missing or Conflicting Maven Dependencies
Even if the class exists in a jar, it might not be on your runtime classpath, or a dependency conflict is preventing it from being loaded.
Fixes:
- Explicitly Add the Transaction API: Add this dependency to your pom.xml to guarantee the class is available:
<dependency> <groupId>javax.transaction</groupId> <artifactId>javax.transaction-api</artifactId> <version>1.3</version> </dependency> - Use Spring Boot Starters: If you're using Spring Boot, leverage a JTA starter to manage dependencies automatically:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId> </dependency> - Check for Conflicts: Run
mvn dependency:treeto identify if any dependencies are excludingjavax.transaction-api. If you find conflicts, exclude them with:<exclusions> <exclusion> <groupId>com.some.conflicting.group</groupId> <artifactId>conflicting-artifact</artifactId> </exclusion> </exclusions>
3. Packaging Issues
If you're building the jar yourself, there might be a problem with how dependencies are bundled.
Fixes:
- Rebuild Your Jar: Run
mvn clean packageto clear cached builds and ensure all dependencies are correctly included. - Verify Spring Boot Packaging: If using Spring Boot, confirm you're using the
spring-boot-maven-plugin—it handles executable jar packaging correctly, ensuring all dependencies are accessible at runtime.
Final Notes
Start with checking your JDK version and adding the explicit javax.transaction-api dependency—these are the most common fixes for this issue. If that doesn't resolve it, move on to verifying module flags and dependency conflicts.
内容的提问来源于stack exchange,提问作者Robin

