Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 466
Giggs Avatar asked Apr 10 '19 06:04

Giggs


3 Answers

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>
like image 159
User Avatar answered Oct 23 '22 03:10

User


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

like image 32
Andrei Sfat Avatar answered Oct 23 '22 03:10

Andrei Sfat


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>
like image 21
Mouad Tahir Avatar answered Oct 23 '22 04:10

Mouad Tahir