【问题标题】:Implementation of JAXB-API has not been found(when running java jar)未找到 JAXB-API 的实现(运行 java jar 时)
【发布时间】:2020-07-21 07:24:59
【问题描述】:

我在行中遇到错误(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.");
    }
}

当它作为测试用例执行时运行良好:

 mvn install

或 mvn:spring:boot 运行

但是当使用 jar 运行时会导致问题:

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

运行 java -jar 并使用 postman 命中时出错。

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

java.io.IOException: error while marshalling authentication.

版本信息

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版本,mvn,一切都一样)。我已将这些依赖项添加到 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 有什么问题?

我什至尝试使用具有以下配置的 mvn 包创建胖 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>

但仍然遇到同样的错误。

【问题讨论】:

  • 你需要将 jaxb-impl 添加到类路径
  • @areus 为什么我需要手动添加?使用 mvn:spring-boot run 运行良好,但仅在使用 java -jar 命令时才会出现问题。
  • Maven 负责将所有依赖项添加到类路径中。如果您想使用java -jar 运行,您要么需要手动将依赖项添加到类路径,要么必须配置 maven 以创建 uber jar。见baeldung.com/deployable-fat-jar-spring-boot
  • @areus 感谢您的回复。我正在使用 mvn install 创建 jar,我认为它会创建一个带有依赖项的胖 jar。

标签: java spring spring-boot jaxb jax-ws


【解决方案1】:

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

请分享结果。

【讨论】:

  • 我知道这个事实。我已经从stackoverflow的答案中尝试过这个东西,但没有用。同样的错误。
  • @VivekSadh :我尝试将应用程序带到相同的错误,但这是由于 pom.xml 或 classpath 中缺少 jaxb-impl 工件。因此,请检查发生此问题的类路径。否则,如果您可以将其复制到虚拟情况,则可以进行更多分析。
【解决方案2】:

1) 仔细检查您没有重复的依赖项,因为 JAXB 工件已被多次重命名。

运行 'mvn dependency:tree' 不应多次输出 JAXB 相关工件。

2) 尝试使用这个神器:

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.2</version>
    </dependency>

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 2017-08-06
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2017-07-24
    • 2020-03-02
    相关资源
    最近更新 更多