Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of JAXB-API has not been found(when running java jar)

I am getting error in the line (JAXBContext.newInstance):

    @Override
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
    SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();

    try {
        JAXBContext context = JAXBContext.newInstance(AuthHeader.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(authentication, soapHeader.getResult());

    } catch (JAXBException e) {
        System.out.println(e.getMessage());
        throw new IOException("error while marshalling authentication.");
    }
}

While it runs fine when this is executed as test case using:

 mvn install

or mvn:spring:boot run

But causes issue when the jar is run using:

java -jar target/fileName-0.0.1-SNAPSHOT.jar 

Error when running java -jar and hit using postman.

Implementation of JAXB-API has not been found on module path or classpath.

java.io.IOException: error while marshalling authentication.

Version info

mvn -version
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-1051-aws", arch: "amd64", family: "unix"

java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

Same jar runs fine on development server but doesn't run on staging server. Both development and staging server have same environment (java version, mvn, everything is same). I have these dependencies added to pom.xml:

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.3</version>
    </dependency>

If it's related to fat jar then why other dependencies like amazon s3, stripe etc. works with normal jar. What's the issue with jaxb only ?

I even tried to create fat jar using mvn package with the following config:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <mainClass>com.patracorp.patrapay.PatrapayApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
            </execution>
        </executions>
    </plugin>

but still getting the same error.

like image 664
Vivek Sadh Avatar asked Apr 08 '20 19:04

Vivek Sadh


People also ask

Why was JAXB removed?

JAXB was integrated within the JVM itself, so you didn't need to add any code dependency to have the functionality within your application. But with the modularization performed in JDK 9, it was removed as the maintainers wanted to have a smaller JDK distribution that only contains the core concepts.

What is JAXB in java?

Java Architecture for XML Binding (JAXB) provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications.

Is JAXB deprecated?

In Java SE 9 and 10, the module java.se.ee, which contains JAX-WS, JAXB and a few other Java EE APIs, is still included but is deprecated, and not included in the module path by default. You need to explicitly enable it if you want to use it.


2 Answers

1) Double check that you don't have duplicated dependencies, because JAXB artifacts had been renamed multiple times.

Running 'mvn dependency:tree' should not output JAXB related artifacts more than once.

2) Try to use this artifact:

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.2</version>
    </dependency>
like image 189
Alex Chernyshev Avatar answered Sep 23 '22 23:09

Alex Chernyshev


In Java 9 and higher versions, Java has removed java.xml.bind from its default class path. So, you have to explicitly add JAR files to the classpath.

And adding only jaxb-impl won't suffice, I guess.
Can you add below dependency in your POM and then try it out.

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>${jaxb-api.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>${jaxb-api.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>${jaxb-api.version}</version>
    </dependency>

Please share the results.

like image 33
Tarun Avatar answered Sep 23 '22 23:09

Tarun