【问题标题】:Eclipse: can't find javax.xml.datatype.XMLGregorianCalendar - Java 11Eclipse:找不到 javax.xml.datatype.XMLGregorianCalendar - Java 11
【发布时间】:2019-08-12 05:18:44
【问题描述】:

我已经将一个项目从 java 1.8 迁移到 java 11。在此过程中,我不得不安装更新版本的 eclipse 来获取 java 11 jdk。

Eclipse IDE for Enterprise Java Developers.
Version: 2018-12 (4.10.0)
Build id: 20181214-0600

应用程序是一个 maven 项目,可以从命令行成功编译,但是 eclipse 抱怨它无法解析 javax.xml.datatype.XMLGregorianCalendar

我已经使用右键单击项目 Maven -> 更新项目从 Eclipse 更新了 maven 项目。

我可以从项目中看到 maven 依赖项 jaxp-api-1.4.2.jar 包含在内,并且 javax.xml.datatype.XMLGregorianCalendar 类存在。

如何解决 Eclipse 抱怨类 javax.xml.datatype.XMLGregorianCalendar 无法解决的问题?

具有未解析的 javax.xml.datatype.XMLGregorianCalendar 的类:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.11 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2018.08.17 at 01:14:19 PM BST 
//


package com.qlsdistribution.fps.production.contentagent;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;



/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType&gt;
 *   &lt;complexContent&gt;
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
 *       &lt;sequence&gt;
 *         &lt;element name="From" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/&gt;
 *         &lt;element name="To" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/&gt;
 *       &lt;/sequence&gt;
 *     &lt;/restriction&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "from",
    "to"
})
@XmlRootElement(name = "JobAPI_GetArchivedJobs")
public class JobAPIGetArchivedJobs {

    @XmlElement(name = "From")
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar from;
    @XmlElement(name = "To")
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar to;

    /**
     * Gets the value of the from property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getFrom() {
        return from;
    }

    /**
     * Sets the value of the from property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setFrom(XMLGregorianCalendar value) {
        this.from = value;
    }

    /**
     * Gets the value of the to property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getTo() {
        return to;
    }

    /**
     * Sets the value of the to property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setTo(XMLGregorianCalendar value) {
        this.to = value;
    }

}

pom.xml 的精简版:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qlsdistribution.fps</groupId>
    <artifactId>fpsproduction</artifactId>
    <version>1.0.53</version>
    <packaging>jar</packaging>

    <name>fps-production</name>
    <description>FocalPoint Sever Production Tool</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.11</java.version>
        <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
        <build.unknown>UNKNOWN</build.unknown>
        <build.number>${build.unknown}</build.number>
        <build.revision>${build.unknown}</build.revision>
        <build.time>${maven.build.timestamp}</build.time>

    </properties>
    <repositories>
        <repository>
            <id>jcenter</id>
            <url>https://jcenter.bintray.com/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- javax.validation  -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.xml/jaxp-api -->
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxp-api</artifactId>
            <version>1.4.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.annotation/jsr250-api -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <release>11</release>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>6.2</version> 
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                        <manifestEntries>
                            <Implementation-Version>${project.version}-${build.number}(${build.time})</Implementation-Version>
                            <Implementation-Build>${build.number}</Implementation-Build>
                            <Implementation-SCM-Revision>${build.revision}</Implementation-SCM-Revision>
                            <Build-Time>${build.time}</Build-Time>
                        </manifestEntries>
                    </archive>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <release>11</release>  
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.8.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/bundle.xml</descriptor>
                    </descriptors>
                    <release>11</release>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <release>11</release>  
                </configuration>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.outputDirectory}/static/docs
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${project.build.directory}/generated-docs
                                    </directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>

                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>

        <!-- Dev Tools -->
        <profile>
            <!-- Need to specify normal profile as well when using this one -->
            <id>devtools</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-devtools</artifactId>
                    <optional>true</optional>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <filter>dev</filter>
            </properties>
        </profile>

    </profiles>
</project>

【问题讨论】:

  • 请出示您的pom.xml 和相关代码sn-p。
  • @mle 代码从命令行成功构建并运行,所以问题不在于代码或 pom,而在于 eclipse。我将更新问题以包含代码和 pom.xml 的相关部分。我已经读过它可能与较旧的 java 版本有关,包括 jdk 中的 jax,并且与 jaxp-api-1.4.2.jar 的 maven 包含冲突,但还没有找到解决方法。
  • Window > Preferences: Java > Installed JREs中添加新安装的Java JDK/JRE。确保在项目中使用 JavaSE-11 作为执行环境。
  • 啊,好吧,换句话说,您的源和目标级别是&lt;java.version&gt;11&lt;/java.version&gt;,对吗?那么我的问题是:你的JAVA_HOME 是如何设置的,分别在你的 Eclipse 运行的哪个 JVM 中?
  • 我遇到了同样的问题,可以确认它也发生在 Oracle JDK 11.0.2 和适用于 Windows 的 Open JDK 11.0.2 上(因为类是一部分,所以不需要 Maven 依赖项JDK)。请注意:调用 Project -> Clean...Maven -> Update Project... 暂时解决了我的问题。

标签: java eclipse maven java-11


【解决方案1】:
  1. 确保在 Window > Preferences: Java > Installed JREs (或在 macOS Eclipse > Preferences: Java > Installed JREs)
  2. 确保在项目中JavaSE-11被用作执行环境:
    1. 在您的 pom.xml 中,将行
      &lt;java.version&gt;1.11&lt;/java.version&gt;
      替换为以下两行:
      &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
      &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
    2. 右键单击 项目 并选择 Maven > 更新项目...
  3. 如果使用Java Platform Module System (JPMS)(= 默认包包含文件module-info.java),请确保module-info.java 包含行requires java.xml;

→ 在Package ExplorerProject Explorer 中显示子节点JRE System Library [JavaSE-11]

【讨论】:

  • 我已按照建议将 maven 设置添加到 pom 中,并添加了项目的屏幕截图并安装了 jre 的设置,这些设置似乎都设置正确。只是要注意我使用的 Eclipse 版本没有 Windows -> Preferences 等。Preferences 来自 Eclipse -> Preferences。
  • @karen 你的默认包中有module-info.java 文件吗?
  • 目前还没有 module-info.java。我不认为这是必需的。
  • @karen No module-info.java 在项目中无处可去?在你pom.xml 里面还有&lt;java.version&gt;1.11&lt;/java.version&gt;。请显示您的 Java Build Path 选项卡 Libraries
  • 没有。作为迁移的一部分,我确实在某个阶段添加了一个,但删除了它,因为我觉得它是我不太了解的东西,所以会在稍后阶段研究它。奇怪的是 eclipse 只抱怨这一类导入。
【解决方案2】:

我有同样的问题,我刚刚通过选择项目并按下鼠标的第二个按钮来制作 maven -> 更新项目。希望对你有帮助

【讨论】:

  • 您可以通过准确说出“进行 maven 更新”的含义来改进此答案。什么命令?什么背景/情况?提供一些细节。
  • maven -> 通过选择项目并按下鼠标的第二个按钮来更新项目。希望能帮到你
【解决方案3】:

您的类型javax.xml.datatype.XMLGregorianCalendar

可能存在重复的依赖项

试试这里发布的这个答案 -> https://stackoverflow.com/a/61387697/11244881

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多