Javax xml bind jaxbexception ошибка

The JAXB APIs are considered to be Java EE APIs and therefore are no longer contained on the default classpath in Java SE 9. In Java 11, they are completely removed from the JDK.

Java 9 introduces the concepts of modules, and by default, the java.se aggregate module is available on the classpath (or rather, module-path). As the name implies, the java.se aggregate module does not include the Java EE APIs that have been traditionally bundled with Java 6/7/8.

Fortunately, these Java EE APIs that were provided in JDK 6/7/8 are still in the JDK, but they just aren’t on the classpath by default. The extra Java EE APIs are provided in the following modules:

java.activation
java.corba
java.transaction
java.xml.bind  << This one contains the JAXB APIs
java.xml.ws
java.xml.ws.annotation

Quick and dirty solution: (JDK 9/10 only)

To make the JAXB APIs available at runtime, specify the following command-line option:

--add-modules java.xml.bind

But I still need this to work with Java 8!!!

If you try specifying --add-modules with an older JDK, it will blow up because it’s an unrecognized option. I suggest one of two options:

  1. You can set any Java 9+ only options using the JDK_JAVA_OPTIONS environment variable. This environment variable is automatically read by the java launcher for Java 9+.
  2. You can add the -XX:+IgnoreUnrecognizedVMOptions to make the JVM silently ignore unrecognized options, instead of blowing up. But beware! Any other command-line arguments you use will no longer be validated for you by the JVM. This option works with Oracle/OpenJDK as well as IBM JDK (as of JDK 8sr4).

Alternate quick solution: (JDK 9/10 only)

Note that you can make all of the above Java EE modules available at run time by specifying the --add-modules java.se.ee option. The java.se.ee module is an aggregate module that includes java.se.ee as well as the above Java EE API modules. Note, this doesn’t work on Java 11 because java.se.ee was removed in Java 11.


Proper long-term solution: (JDK 9 and beyond)

The Java EE API modules listed above are all marked @Deprecated(forRemoval=true) because they are scheduled for removal in Java 11. So the --add-module approach will no longer work in Java 11 out-of-the-box.

What you will need to do in Java 11 and forward is include your own copy of the Java EE APIs on the classpath or module path. For example, you can add the JAX-B APIs as a Maven dependency like this:

<!-- API, java.xml.bind module -->
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.2</version>
</dependency>

<!-- Runtime, com.sun.xml.bind module -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.2</version>
</dependency>

See the JAXB Reference Implementation page for more details on JAXB.

For full details on Java modularity, see JEP 261: Module System

As of July 2022, the latest version of the bind-api and jaxb-runtime is 4.0.0. So you can also use

    <version>4.0.0</version>

…within those dependency clauses. But if you do so, the package names have changed from javax.xml.bind... to jakarta.xml.bind.... You will need to modify your source code to use these later versions of the JARs.

For Gradle or Android Studio developer: (JDK 9 and beyond)

Add the following dependencies to your build.gradle file:

dependencies {
    // JAX-B dependencies for JDK 9+
    implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
    implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"
}

If you are upgrading your Java application from a lower version to Java 11, you may get the following error:

java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
...
...
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
...
...
...

The JAXB APIs are considered to be Java EE APIs, and therefore are no longer contained on the default class path in Java SE 9. In Java 11 they are completely removed from the JDK. If you are using Java 9, then the JAVA SE module named java.se is in the classpath but this module doesn’t contain the JAVA EE APIs. While from Java 11 onwards, the jaxb apis, namely:

  1. jaxb-api

  2. jaxb-core

  3. jaxb-impl

  4. activation, etc

are completely removed from the Java installation. Hence, to resolve the above error, you must include the jaxb-api jar file in the classpath of your project/application.

For Maven Projects:

If you are using Maven for handling dependencies in your Java project, you will have to add the following additional dependency in your pom.xml file.

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

This will include the jaxb-api Jar file in your project when you will build your maven java project.

For Gradle Projects:

If you use Gradle to build your project, then add the following line to your build.gradle file,

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'

For SBT Projects:

If you use SBT build tool to compile and build your java project, then add the following line to your build file,

libraryDependencies += "javax.xml.bind" % "jaxb-api" % "2.3.0"

For IVY Projects:

If you use ivy for building your java project, then add the following code line to your ivy.xml file:

<dependency org="javax.xml.bind" name="jaxb-api" rev="2.3.0"/>

If you don’t use any build tool

If you are not using any build tool, then you can download the jar file from the following link: JAXB-API 2.3.0 MVN Repository and add it to your project’s classpath

Once you have made the following changes, restart your Java application, and the error should be resolved. So this was the solution for the java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException Error. If you are not able to understand anything, feel free to comment, and we will help you resolve your error.

Conclusion

To summarise, the «java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException» problem in Java 11 is irritating, but it is fixable. This error happens as a result of the loss of JAXB from Java 11, but it is fixable by adding the required dependencies to your project. You can effectively resolve this error and proceed with your Java 11 development tasks by following the methods outlined in this piece. Remember to maintain your files up to date to avoid making similar mistakes in the future.

Frequently Asked Questions

1. What is the cause of «java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException» in Java 11?

Java 11 removed the JAXB (Java Architecture for XML Binding) module, causing this error when running code that depends on it.

2. How can I resolve «java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException» in Java 11?

You can resolve this error by adding the JAXB module to your project’s dependencies. For example, if using Maven, add the following dependency to your pom.xml file:

<dependencies>
    <!-- Other dependencies -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
</dependencies>

3. Can I still use JAXB in Java 11?

You can still use JAXB in Java 11 by adding it as a separate module or dependency to your project.

4. What are the alternatives to JAXB in Java 11?

Alternatives to JAXB in Java 11 include using other XML binding frameworks such as Jackson or XMLBeans, or manually parsing XML using the built-in XML parsing capabilities of Java.

5. What is the error in Javax XML bind?

The error in Javax XML bind typically occurs when the JAXB library is not included in the classpath or when the version of the library is not compatible with the version of the Java runtime environment.

You Might Also Like

  • [SOLVED] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

  • [SOLVED] Caused by: java.lang.ClassNotFoundException: javax.xml.ws.WebServiceFeature in Java 11

  • How to convert ZonedDateTime to Date in Java?

  • Log4j2 Programmatic Configuration in Java Class

JAXBException is the root exception class for all the JAXB exceptions. Generally this exception occurred when the implemented code is not fulfill the pre-requisite for JAXB related dependencies.

Constructors

JAXBException class having following constructors.

  • JAXBException(String msg): Construct exception with detail message..
  • JAXBException(String msg, String errorCode) : This construct exception with detail message with error code.
  • JAXBException(String msg, String errorCode, Throwable exception) : Construct detail message with error code and stacktrace of linked exception stacktrace.
  • JAXBException(String msg, Throwable exception) :Construct exception with detail message and stacktrace of linked exception.
  • JAXBException(Throwable exception) : Construct exception with stacktrace of linked exception.

Problem

This JAXBException occurred in your code because on runtime JAXB is not able to find JAXB-API related dependency.

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
	at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
	at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
	at com.facingIssuesOnIT.domain.EmployeeJAXBTest.marshal(EmployeeJAXBTest.java:23)
	at com.facingIssuesOnIT.domain.EmployeeJAXBTest.main(EmployeeJAXBTest.java:15)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
	at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
	at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
	... 5 more

Solutions

To implement JAXB marshalling and unmarshalling in your application, you need to add following dependencies in you code. As above exception message explained “JAXB-API has not found in module or class path” this exception occurred when you forget to add JAXB-API dependency.

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>

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

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.1</version>
</dependency>
 
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.25.0-GA</version>
</dependency>

Conclusion

In this post , You learn about the JAXBException cases to occurred, different constructors and example to solve JAXB-API has not found in module or class path” JAXBException.

Let me know your thought to about this post.

Happy Learning !!!

“Learn From Others Experience»

Hi, I am creating application using Spring and Hibernate. I am using Java 9 and Spring 5.0.1. While starting server, there is following error in console.

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)

at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:87)

at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:208)

at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:163)

at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:51)

at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:358)

at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:384)

at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:373)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1763)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1700)

… 25 common frames omitted

Mohit

Replied on November 11, 2017

JDK 9 has done some changes for java.xml.bind and other java EE modules. Let us understand.

1.

JDK 9 has deprecated java.xml.bind module and has removed from default classpath.

@Deprecated(since=»9″, forRemoval=true)

Module java.xml.bind

Deprecated, for removal: This API element is subject to removal in a future version.

https://docs.oracle.com/javase/9/docs/api/java.xml.bind-summary.html

Java has made plan to remove the Java EE and CORBA modules from Java SE and the JDK after JDK 9 versions. In Java 9 they have only deprecated and have removed it from classpath.

2.

javax.xml.bind is sub package of Module java.xml.bind

So Module javax.xml.bind will not be available on classpath by default in JAVA 9.

Solution:

1. Use —add-modules to add module in classpath.

As Java has not yet removed from module from java 9. Java has only deprecated and does not add javax.xml.bind module on classpath by default.

So if we want to add javax.xml.bind on classpath we can add using following command.

—add-modules java.xml.bind

2. We can use Maven and Gradle to include javax.xml.bind in our project.

Maven

<dependency>

  <groupId>javax.xml.bind</groupId>

  <artifactId>jaxb-api</artifactId>

  <version>2.3.0</version>

</dependency> 

Gradle

compile ‘javax.xml.bind:jaxb-api:2.3.0’

Find the reference link

http://openjdk.java.net/jeps/8189188

Понравилась статья? Поделить с друзьями:
  • Javascript сообщение об ошибке
  • Javascript подавление ошибок
  • Javascript ошибка языка
  • Javascript ошибка соединения
  • Javascript обработчик ошибок