17

I have problem when trying to add JPA to my Spring Boot program. Everything is working fine until I added the below to my pom.xml (I haven't added any code related to JPA Hibernate yet)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

I got the below error when running the created jar file. Compiling with mvn clean package returning no error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at hello.App.main(App.java:19) [classes!/:0.1.0]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [gs-consuming-rest-0.1.0.jar:0.1.0]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [gs-consuming-rest-0.1.0.jar:0.1.0]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [gs-consuming-rest-0.1.0.jar:0.1.0]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [gs-consuming-rest-0.1.0.jar:0.1.0]

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:87) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:179) ~[hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:149) ~[hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54) ~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
... 23 common frames omitted


Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563) ~[na:na]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[gs-consuming-rest-0.1.0.jar:0.1.0]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496) ~[na:na]
... 33 common frames omitted

If I remove the spring-boot-starter-data-jpa then everything works fine.

Please help or guide me how to check.

I have looked through all the replies here Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed but found nothing to work for my case

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nguyen Pham
  • 444
  • 1
  • 4
  • 14
  • And `ClassNotFoundException: javax.xml.bind.JAXBException` ? Maybe you need to put that in the CLASSPATH ... –  Nov 27 '17 at 09:24

4 Answers4

32
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

Add this dependency in pom.xml

The latest version could be found here : mvnrepository.com

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
  • Thanks... I will try to add that and update. Just want to check, for all the tutorials I have looked through, not one mentions the requirement for adding xml and eclipselink dependencies. Is it running differently if I create the project in Eclipse and other IDEs? – Nguyen Pham Nov 27 '17 at 07:51
  • 1
    Thanks a lot. It's working now. Do you know if it's included in any other jars? Since I look through online tutorials noone is mentioning to include this dependency. – Nguyen Pham Nov 27 '17 at 11:27
  • 4
    It isn't mentioned in another tutorial because those are al java8 based and JDK8 includes those classes by default. However you are using Java 9 (which isn't actually supported by Spring Boot yet!) which doesn't include JAXB anymore... – M. Deinum Nov 27 '17 at 11:39
  • oh... I see now... eye-opening for me... thanks again – Nguyen Pham Nov 27 '17 at 11:53
  • 1
    This solution worked for me as I followed a Spring Boot tutorial on YouTube. – AryanJ-NYC May 13 '18 at 23:20
  • 1
    Thanks !! This solution works great with Spring Boot and Java 10. – Boogieman Aug 14 '18 at 19:24
1

Your dependencies is conflicted i thought. You can check it using the command in console.

mvn dependency:tree -Dverbose

Good luck.

David Pham
  • 1,673
  • 19
  • 17
  • Oh thanks. I remember checking in Eclipse and see some jar omitted but don’t know Dverbose so my mvn tree looks normal. I also tried to exclude some omitted jar but still not working. How should I check with entitybeanmanager version I should use though? – Nguyen Pham Nov 27 '17 at 05:31
  • You mean you remove jpa then it works fine. So it you don't use it properly in spring boot application possibly. You can refer to the way using Jpa in spring boot in the post http://javabycode.com/spring-framework-tutorial/spring-boot-tutorial/spring-boot-jpa-one-to-many-relationship-mapping-example.html – David Pham Nov 27 '17 at 07:44
  • I have not put any JPA-related code in the other classes yet. What I mean for "removing JPA then it works fine" is that in my pom.xml file, if I don't add the dependency for "spring-boot-starter-data-jpa" then the Application compiles and runs correctly. So most likely is the wrong configuration but I don't know how to check the required version and what is missing yet – Nguyen Pham Nov 27 '17 at 07:49
  • How do I know if the dependencies are conflicting? – Philip Rego Feb 06 '18 at 00:35
0

tried this following line in VM arguments in any IDE like eclipse or STS

--add-modules java.xml.bind

Good Luck ...!!

Raj
  • 93
  • 1
  • 10
-1

You can Resolved this by using rt.jar file import in project lib No 1 Right Click on Your Project go to Properties

No 2 Click on Java Build Path

Then See at right side Click On Add Variables

No 3 Open a new Window Click on Extent button

No 4 C:\Program Files\Java\jre1.8.0_111\lib\rt.jar set path and give name of variable(aaa) not issue of variable name your can your name asif