Eclipse: can't find javax.xml.datatype.XMLGregorianCalendar - Java 11
Asked Answered
L

4

19

I have migrated a project from java 1.8 to java 11. In the process I have had to install a newer version of eclipse to pick up the java 11 jdk.

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

The application is a maven project and compiles successfully from the command line, however eclipse complains it can't resolve javax.xml.datatype.XMLGregorianCalendar.

I have updated the maven project from eclipse using right click on project Maven -> Update Project.

I can see from the project maven dependencies jaxp-api-1.4.2.jar is included and that the class javax.xml.datatype.XMLGregorianCalendar exists.

How do I fix the issue with Eclipse complaining the class javax.xml.datatype.XMLGregorianCalendar can't be resolved?

Class with unresolved 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;
    }

}

Stripped down version of 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>

enter image description here

enter image description here

enter image description here

Lissalissak answered 21/3, 2019 at 11:1 Comment(10)
Please show your pom.xml and a relevant code snippet.Spent
@Spent The code builds and runs successfully from the command line, so the issue isn't with the code or pom but eclipse. I will update the question to include the relevant parts of the code and pom.xml. I have read that it can be to do with older java versions including jax within the jdk and clashing with the maven inclusion of the jaxp-api-1.4.2.jar but haven't found how to resolve this.Lissalissak
Add the new installed Java JDK/JRE in Window > Preferences: Java > Installed JREs. Make sure in the project JavaSE-11 is used as execution environment.Popp
Ah ok, so your in other your words, your source and target level is <java.version>11</java.version>, correct? Then my question would be: How is your JAVA_HOME set, respective in what JVM does your Eclipse run?Spent
The installed JRE's are all correct and are using the installed version: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home. However I have just realised when I run maven I have a special setting (in .bashrc) to change the JAVA_HOME setting for the appropriate version: alias mvn11="JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home && mvn" but my default JAVA_HOME is /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home. So how can I change JAVA_HOME just for this eclipse?Lissalissak
@Lissalissak The JRE/JDK to run Eclipse can be different from the JRE/JDK used by a project inside of Eclipse. Have added Java 11 in Window > Preferences: Java > Installed JREs? Is Java used in the project (in the Package Explorer is there the subnode JRE System Library [JavaSE-11])?Popp
@Popp The installed JRE's is set by default to use /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home and the project is set for the sameLissalissak
@Lissalissak The default only matters if you start Eclipse with a new workspace.Popp
I'm facing the same problem and can confirm that it happens on Oracle JDK 11.0.2 and Open JDK 11.0.2 for Windows as well (for them Maven dependencies are not needed because the class is part of the JDK). Just a note: calling Project -> Clean... and Maven -> Update Project... temporarily fixed the issue for me.Twain
Hi , I am facing an identical problem. I have some auto generated classes that are not able to acces the javax.xml.datatype.XMLGregorianCalendar. It throws, class not found Exception when i run the application. I have included the java 11 , my java -version is java 11. Did you find a solution to this problem?Chessboard
P
5
  1. Make sure the new installed Java JDK 11 in Window > Preferences: Java > Installed JREs (or on macOS Eclipse > Preferences: Java > Installed JREs)
  2. Make sure in the project JavaSE-11 is used as execution environment:
    1. In your pom.xml replace the line
      <java.version>1.11</java.version>
      with the following two lines:
      <maven.compiler.source>11</maven.compiler.source>
      <maven.compiler.target>11</maven.compiler.target>
    2. Right-click the project and choose Maven > Update Project...
  3. If using the Java Platform Module System (JPMS) (= the default package contains the file module-info.java), make sure module-info.java contains the line requires java.xml;

→ In the Package Explorer or Project Explorer the subnode JRE System Library [JavaSE-11] is displayed

Popp answered 21/3, 2019 at 13:27 Comment(9)
I have added the maven settings to the pom as suggested and have added screenshots of the project and installed jre's settings which all appear to be set correctly. Just to note the eclipse version I am using doesn't have Windows -> Preferences etc. Preferences is from Eclipse -> Preferences.Lissalissak
@Lissalissak Do you have a module-info.java file in your default package?Popp
No module-info.java as yet. I didn't think this was required.Lissalissak
@Lissalissak No module-info.java nowhere in the project? In you pom.xml there is still <java.version>1.11</java.version>. Please show your Java Build Path tab Libraries.Popp
No. I did add one at one stage as part of migrating but removed it as I felt it was something I don't really know enough about, so would look into it at a later stage. The strange thing is eclipse is only complaining about this one class import.Lissalissak
@Lissalissak Please check also your test folder for a module-info.java file and show your Java Build Path tab Libraries.Popp
Let us continue this discussion in chat.Lissalissak
I double check all your suggestion you gave in question,comments and chat, but still it is not working. It is very strange I have the java 11, but for somehow , javax.xml.datatype.XMLGregorianCalendar is not being recognized.Chessboard
@EmaP. Then the issue is probably caused by something else. Do you have a module-info.java file, and if so, does it contain requires java.xml;? Make also sure, not to have a dependency with the javax.xml package, since this is not allowed in Java 9 and higher (independent of whether or not having a module-info.java file).Popp
S
1

Possibly there are duplicate dependency present for your type javax.xml.datatype.XMLGregorianCalendar

Try this answer posted here -> https://mcmap.net/q/668492/-jdk-10-cannot-import-javax-xml-namespace-in-eclipse

Subsequent answered 28/6, 2020 at 10:58 Comment(0)
E
0

This can also happen if javax.xml is accessible from more than one module. You might see this error somewhere in the IDE itself (Or if you hover over the error in the IDE you can see more information)

Or you can simply press Ctrl + Shift + T in Eclipse to open up the Open Type prompt. Then type javax.xml.datatype or javax.xml.datatype.XMLGregorianCalendar to see where Eclipse is loading those from.

javax.xml is already in JRE, so you might want to remove all other resources which give javax.xml from the classpath. You might need to exclude some of the libraries which has java.xml from the pom.xml itself.

Eba answered 8/3 at 17:38 Comment(0)
M
-2

I have the same problem I just solved by making maven -> update project by selecting the project and pressing the second button of the mouse. I hope it can help

Metonymy answered 28/1, 2020 at 11:55 Comment(2)
You can improve this answer by saying exactly what you mean by "making maven update". What command? What context / situation? Give some details.Walczak
maven -> update project by selecting the project and pressing the second button of the mouse. I hope it can helpMetonymy

© 2022 - 2024 — McMap. All rights reserved.