14

Getting the following exception , while implementing Jwt Spring Security implementation :

java.lang.NoClassDefFoundError: Could not initialize class javax.xml.bind.DatatypeConverterImpl .

in a Spring Boot project

The dependencies in pom.xml are as following :

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- For Working with Json Web Tokens (JWT) -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- marked the embedded servlet container as provided -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- hot swapping, disable cache for template, enable live reload -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.3.3.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- Optional, for bootstrap -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <scope>compile</scope>
        <version>1.0</version>
    </dependency>
    
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.modelmapper</groupId>
        <artifactId>modelmapper</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version>
    </dependency>

</dependencies>

The detailed bootstrap is as following :

Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: 
Could not initialize class javax.xml.bind.DatatypeConverterImpl
] with root cause
java.lang.NoClassDefFoundError: Could not initialize class javax.xml.bind.DatatypeConverterImpl
    at javax.xml.bind.DatatypeConverter.initConverter(Unknown Source)
    at javax.xml.bind.DatatypeConverter.parseBase64Binary(Unknown Source)
    at io.jsonwebtoken.impl.Base64Codec.decode(Base64Codec.java:26)
    at io.jsonwebtoken.impl.DefaultJwtBuilder.signWith(DefaultJwtBuilder.java:99)
    at com.mpcz.services.jwt.security.JwtTokenUtil.generateToken(JwtTokenUtil.java:92)
    at com.mpcz.services.jwt.security.JwtTokenUtil.generateToken(JwtTokenUtil.java:85)
    at com.mpcz.services.controller.ConsumerLoginController.register(ConsumerLoginController.java:183)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Getting the above mentioned error while generating JWT token at below mentioned code :

Jwts.builder().setClaims(claims).setExpiration(generateExpirationDate()).signWith(SignatureAlgorithm.HS512, secret).compact();
Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
Giggs
  • 203
  • 1
  • 2
  • 15

5 Answers5

29

For future reference, the solution was adding the following dependency in order to avoid the java 11 issue:

For Maven projects:

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

For gradle projects:

compile 'javax.xml.bind:jaxb-api:2.1'

Credits to @Giggs

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
6

Adding JAXB API dependency solved my issue.

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
User
  • 1,460
  • 14
  • 11
1

Adding this dependency to the pom file worked for me :

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version><!--$NO-MVN-MAN-VER$-->
</dependency>
Mouad Tahir
  • 450
  • 6
  • 9
0

That also worked just fine for me while using OpenJDK 11. w/o any code modification.

I am using java.base.* enc/dec on my fronted class instead of javax.xml.bind.DatatypeConverter, but jjwt (0.9.0) brings it back in when generating the token.

your solution is good, did not have to add the Xercesxml to my pom.xml, probably because I was only doing basic jwt.

Regards,

Dezman!

Dezman
  • 1
  • 1
0

Yes, For me adding javax api dependency fixed my bug.

compile 'javax.xml.bind:jaxb-api:2.1'

Thanks @Andrei