实现JAXB-API未找到(在运行Java Jar时)

huangapple go评论71阅读模式
英文:

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

问题

我在这行代码中遇到错误 (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("在编组认证信息时出错。");
        }
    }

虽然通过以下方式作为测试用例执行时运行正常:

mvn install

或者

mvn spring-boot:run

但是在使用以下命令运行 JAR 文件时会出问题:

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

在运行 java -jar 并通过 Postman 发送请求时出错。

在模块路径或类路径上未找到 JAXB-API 的实现。

java.io.IOException: 在编组认证信息时出错。

版本信息:

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)

同样的 JAR 文件在开发服务器上运行良好,但在演示服务器上无法运行。开发和演示服务器具有相同的环境(Java 版本、Maven 等均相同)。我已将以下依赖项添加到 pom.xml 文件中:

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

如果问题与 fat JAR 有关,那么为什么其他依赖项(如 Amazon S3、Stripe 等)在普通 JAR 中能正常工作?只有 JAXB 存在什么问题?

我甚至尝试使用以下配置创建 fat JAR:

<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>

但仍然遇到相同的错误。


<details>
<summary>英文:</summary>

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(&quot;error while marshalling authentication.&quot;);
        }
    }


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: &quot;linux&quot;, version: &quot;4.15.0-1051-aws&quot;, arch: &quot;amd64&quot;, family: &quot;unix&quot;

    java -version
    openjdk version &quot;11.0.7&quot; 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&#39;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:

        &lt;dependency&gt;
            &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
            &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
            &lt;version&gt;2.3.3&lt;/version&gt;
        &lt;/dependency&gt;

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


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

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

but still getting the same error. 

</details>


# 答案1
**得分**: 12

1) 请仔细检查您的依赖项,确保没有重复的依赖项,因为 JAXB 的相关构件已经多次更名。

运行 'mvn dependency:tree' 不应该多次输出与 JAXB 相关的构件。

2) 请尝试使用以下构件:

```xml
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.2</version>
</dependency>
英文:
  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.

  1. Try to use this artifact:

     &lt;dependency&gt;
         &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
         &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
         &lt;version&gt;2.3.2&lt;/version&gt;
     &lt;/dependency&gt;
    

答案2

得分: 3

在Java 9及更高版本中,Java已从默认类路径中移除了java.xml.bind。因此,您必须显式地将JAR文件添加到类路径中。

仅添加jaxb-impl可能是不够的,我猜想。
您可以在您的POM中添加以下依赖,然后尝试一下。

<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>

请分享结果。

英文:

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.

    &lt;dependency&gt;
        &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
        &lt;version&gt;${jaxb-api.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
        &lt;version&gt;${jaxb-api.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-core&lt;/artifactId&gt;
        &lt;version&gt;${jaxb-api.version}&lt;/version&gt;
    &lt;/dependency&gt;

Please share the results.

huangapple
  • 本文由 发表于 2020年4月9日 03:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61108214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定