繁体   English   中英

未找到 JAXB-API 的实现(运行 java jar 时)

[英]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("error while marshalling authentication.");
    }
}

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

 mvn install

或 mvn:spring:引导运行

但是当 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 package 和以下配置创建 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>

但仍然出现相同的错误。

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

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

2)尝试使用这个神器:

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

在 Java 9 及更高版本中, java.xml.bind已从其默认路径 class 中删除了 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>

请分享结果。

我在行中遇到错误(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>

如果它与胖 jar 相关,那么为什么其他依赖项(如 amazon s3、stripe 等)适用于正常的 jar。 仅 jaxb 有什么问题?

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

但仍然得到同样的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM