8

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.

Anish B.
  • 9,111
  • 3
  • 21
  • 41
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • 1
    You need to add jaxb-impl to the classpath – areus Apr 08 '20 at 19:44
  • @areus Why do I need to manually add it ? It runs fine using mvn:spring-boot run but issue occurs only when using java -jar command. – Vivek Sadh Apr 09 '20 at 04:23
  • 1
    Maven takes care of adding all dependencies to the classpath. If you want to run with `java -jar` you either need to manually add the dependencies to the classpath or you must configure maven to crear an uber jar. See https://www.baeldung.com/deployable-fat-jar-spring-boot – areus Apr 09 '20 at 06:38
  • @areus Thanks for your response. I am using mvn install to create jar and I thought it would create a fat jar with dependencies. – Vivek Sadh Apr 09 '20 at 09:55
  • Can you add the project git repo ? I want to test what is actually happening – Anish B. May 09 '20 at 04:32
  • 1
    Does this answer your question? [Replacements for deprecated JPMS modules with Java EE APIs](https://stackoverflow.com/questions/48204141/replacements-for-deprecated-jpms-modules-with-java-ee-apis) – 9ilsdx 9rvj 0lo Jun 07 '21 at 11:54

2 Answers2

12

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>
Alex Chernyshev
  • 1,719
  • 9
  • 11
3

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.

Tarun
  • 685
  • 3
  • 16
  • I know this fact. I already tried this thing from stackoverflow answers but doesn't work. Same error. – Vivek Sadh May 03 '20 at 17:17
  • @VivekSadh : I tried and brought the application to same error but it was due to missing `jaxb-impl` artifact in pom.xml or classpath . So, Please check your classpath where this issue is happening. Otherwise ,If you can reproduce it to dummy situation , it can be more analyzed. – Tarun May 04 '20 at 19:36
  • @VivekSadh Were you able to figure it out? I tried adding all 6 dependencies and it still doesnt work(jaxb-api, jaxb-impl, jaxb-core, jaxb-runtime, jakarta.xml.bind-api, jakarta.annotation-api)? Tried different permutations of these as suggested by 5-6 SO posts, but none worked so far in Java 17 – theprogrammer Mar 10 '23 at 18:50